📢 Attention Salesforce Certified Trailblazers! Maintain your credentials and link your Trailhead and Webassessor accounts by April 19th. Learn more.
close

Use Visual Studio Code for Salesforce Development

In this step, we explore some of the more advanced features of Visual Studio code, such as how to use the integrated terminal and our newly installed Salesforce Extension Pack.

Terminal Versus Command Palette

Like with any good development tool, there is more than one way to do things with Visual Studio Code. The two main ways you can interact with Salesforce CLI are through the integrated terminal or quick open window.

To view the quick open window, press Command + P on Mac or Ctrl + P on Windows. If you type ? you can view the help menu. Through this module we will use the quick open window in command palette mode, which allows us to show and run commands.

The view of the global commands in the quick open window with ? in the field.

Note

Note

Through the rest of the project, we rely on both the terminal and the quick open window. In order to best prepare you and show you the differences, we show you how to create a project in both ways. You only have to create the project once.

Create a Project

  1. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to make the command palette appear.
  2. Make sure the new prompt starts with >
  3. Type SFDX: Create Project
  4. Note

    Note

    This should autocomplete when you type any part of the command name. If it isn’t working, make sure the prompt starts with > and that you have the Salesforce Extension Pack and Salesforce CLI installed as described in the previous step of this quick start.

  5. Press Enter.
  6. Type: VSCodeQuickstart .
  7. Select your Desktop as the place to create the project in so it is easy to find later on.
  8. Wait for the new Visual Studio Code window to open. You should see an indication that the extension is preparing your project before populating the file explorer.

 Extension notice: Running SFDX: Create Project.

Alternative Flow: Create a Project

  1. Click Terminal on the top options bar.
  2. From the dropdown select New Terminal.
  3. Type sfdx force:project:create --projectname VSCodeQuickStart
  4. Press Enter.
  5. It takes a moment for the terminal to return your results. The command creates four files, sfdx-project.json, README.md, .forceignore, and project-scratch-def.json.

    The four files created in the VS Code terminal, preceded by VSCodeQuickStart/

    Also, if it’s your first time using Salesforce CLI and Extension Pack, you see a notice that the extension pack may collect usage data. We use this data to help make the product better by collecting bugs and other metrics.

    The data notice, “You agree that Salesforce Extensions for VS Code may collect usage information, user environment, and crash reports for product improvements. Learn how to opt out.”

  6. In Visual Studio Code, press Command + O on Mac, or Ctrl + O on Windows.
  7. Choose the newly created VSCodeQuickStart folder.

Search your Files

  1. Press Command + P on Mac or Ctrl + P on Windows to make the search palette appear. This shifts the focus to search files.
  2. Type project-scratch-def.json into the field.
  3. Click the result to open the file.
  4. On the left hand side of Visual Studio code, click the magnifying glass magnifying glass. This is the Find and Replace menu.
  5. Search for orgName.
  6. Click the first time it is found in project-scratch-def.json.
  7. Change the orgName value (after the : and in between the “”) to Learning VS Code.
  8. Save the file by pressing Command + S on Mac, Control + S on Windows.

The project-scratch-def.json file with the new org name.

Authenticate to Your Playground

  1. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to make the command palette appear.
  2. Type SFDX: Authorize an Org.
  3. To accept the default login URL, press Enter.
  4. Enter the alias VSCodePlayground.
  5. Notice that your default browser opens a new Salesforce login window. Log in to your playground using your playground username and password retrieved from the last step.
  6. When you are asked to grant access to the connected app, click to allow.  The Allow Access page for a global connected app
  7. Close the browser window.

Check it out, your successful integration was logged in the output panel!

Successfully authorized USER with org ID/You may now close the browser.

Create an Apex Class

  1. Click the Explorer icon explorer icon in Visual Studio Code to expand the force-app folder.
  2. Under the VSCODEQUICKSTART directory, click force-app to show its folder tree. You should see main, then default, then aura and lwc.
  3. Right click the default folder.  With the default folder right clicked, New Folder is selected in the options list.
  4. Click New Folder.
  5. Name the new folder classesWith default selected, a red arrow is calling out the new folder being named classes.
  6. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to make the command palette appear.
  7. Type SFDX: Create Apex Class.
  8. Enter the name AccountController.
  9. If VS Code asks, select force-app/main/default/classes as the directory you wish to add AccountController.cls to.
  10. In the newly opened AccountController.cls file, replace the default code with the following:
  11. public with sharing class AccountController {
      public static List<Account> getAllActiveAccounts() {
        return [SELECT Id,Name,Active__c FROM Account WHERE Active__c = 'Yes'];
      }
    }
  12. Save your file.

Query  

Our new Apex class has a SOQL query in it, but we want to make sure it works as we expect before we deploy it to our org. We use the command palette to run the query against our org.

  1. In line 3 of the code, highlight the query SELECT Id,Name,Active__c FROM Account WHERE Active__c = 'Yes'
  2. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to make the command palette appear.
  3. Search for SFDX:Execute SOQL Query with Currently Selected Text.
  4. Press Enter.
  5. Select REST API and press Enter.
  6. Note

    Note

    Similar to the other actions you’ve invoked through the extensions pack, it takes a moment for VS Code to return the results.

  7. In the Output tab of the integrated terminal, view the results of your query. You should have also received a notice that states: SFDX: Execute SOQL Query ... ended with exit code 0. This means it successfully ran.

The Output tab displaying the 10 records received from your Trailhead Playground.

Deploy

The last step is to deploy your code to your playground from within Visual Studio Code.

  1. Right click the classes folder.
    With the classes folder right clicked, SFDX: Deploy Source to Org is selected in the options list.
  2. Click SFDX: Deploy Source to Org.
  3. In the Output tab of the integrated terminal, view the results of your deployment. You should have also received a notice that states: SFDX: Deploy Source to Org ... ended with exit code 0. This means it successfully ran.

The Output tab displaying the results with exit code 0 for success.

Parting Thoughts

You’ve learned how to use Visual Studio Code and the Salesforce Extensions. You’ve seen how the features of an IDE can help maximize your development efficiency. Now you’re ready to explore some of the more complex topics, such as debugging with the Apex Reply Debugger, customizing your editor to fit your needs, and running your developer pipelines with Visual Studio Code.

retargeting