Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Use Visual Studio Code for Salesforce Development

Follow Along with Trail Together

Want to follow along with an instructor as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live.

(This clip starts at the 19:41 minute mark, in case you want to rewind and watch the beginning of the step again.)

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 Ctrl+P (Windows) or Cmd+P(macOS). 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.

Create a Project

  1. Press Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS) to make the command palette appear.
  2. Make sure the new prompt starts with >.
  3. Type SFDX: Create Project.
  4. Select SFDX: Create Project.
  5. Select Standard.
  6. Type the project name VSCodeQuickstart and press Enter.
  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.

Search your Files

  1. Press Ctrl+P (Windows) or Cmd+P(macOS) 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. Click the Search () menu.
  5. Search for orgName.
  6. In the first result 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 Ctrl+S (Windows) or Cmd+S (macOS).
    The project-scratch-def.json file with the new org name.

Authenticate to Your Playground

  1. Press Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS) to make the command palette appear.
  2. Type SFDX: Authorize an Org.
  3. Select SFDX: Authorize an Org.
  4. To accept the default login URL, press Enter.
  5. Enter the alias VSCodePlayground.
  6. 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.
  7. When you are asked to grant access to the connected app, click Allow.
  8. Close the browser window.
    The command-line terminal window returns a success message when the transaction is complete.

Create an Apex Class

  1. Click the Explorer () menu.
  2. Under the VSCODEQUICKSTART directory, click force-app to show its folder tree. In the force-app/main/default directory, you find the metadata included within the project such as applications, aura, classes, and more. The folder tree expanded so you can see the classes folder.
  3. Right click the classes folder.
  4. Select SFDX: Create Apex Class.
  5. Enter the name AccountController.
  6. If VS Code asks, select force-app/main/default/classes as the directory you wish to add AccountController.cls to.
  7. In the newly opened AccountController.cls file, replace the default code with the following:
    public with sharing class AccountController {
      public static List<Account> getAllActiveAccounts() {
        return [SELECT Id, Name, Active__c FROM Account WHERE Active__c = 'Yes' WITH SECURITY_ENFORCED];
      }
    }
  8. 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' (don't highlight WITH SECURITY_ENFORCED)
  2. Press Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS) to make the command palette appear.
  3. Type SOQL.
  4. Select SFDX:Execute SOQL Query with Currently Selected Text.
  5. Select REST API and press Enter.
  6. In the Output tab of the integrated terminal window, review the results of your query. The window should state a summary that says: 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.

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 Replay Debugger, customizing your editor to fit your needs, and running your developer pipelines with Visual Studio Code.

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback