How to get started with Visual Studio Code for Salesforce development

Published


Author of this article: Evi Favaits (PwC Salesforce Team)

 

Are you a Salesforce developer? Then follow the guidelines below to get started with Visual Studio Code (VSC), the latest integrated development environment (IDE) for Salesforce.

 

What’s VSC?

It’s a lightweight, modern and free IDE created by Microsoft that provides features for working with development organisations or orgs (scratch orgs, sandboxes, and DE orgs), Apex, Aura components, and Visualforce. It runs on any platform (Windows, Mac and Linux) and it’s free and open source. Extensions are open-source products, fully supported by Salesforce. You can download extensions from the VSC Marketplace.

VSC supports both development models:

1. Org-based development

This development model allows you to work with orgs that don’t have source tracking. You can deploy against any org: sandboxes, developer edition orgs, trailhead orgs and even production. Changes must be tracked manually which allows users utilizing change sets to deploy your code to a sandbox or an production org.

2. Package-based development

This is used for creating self-contained applications that are deployed to your org as a single package, such as scratch orgs. Scratch orgs are lightweight environments for individual developers to build and test their work. This model uses source tracking, so it automatically tracks changes in the org and on your file system.

 

How to set up your environment?

  1. Download and install VSC here.
  2. Download and install the Salesforce command line interface (CLI). This interface is used to run commands against Salesforce orgs. Download the CLI here. Once it’s installed, you can test it by going to the command window and typing ‘sfdx’. It should give a similar result to the one shown below:

 

  1. Install the Salesforce Extensions Pack: this extension can be downloaded in VSC by going to the extension icon on the left side. Search for the Salesforce Extension Pack and click on the first result.

 

This extension pack is a combination of various extensions. Here’s a list of them:

  • Apex
  • Apex interactive debugger
  • Apex replay debugger
  • Salesforce CLI integration
  • Aura components
  • Visualforce
  • Lightning web components

You can also check them by clicking the extension pack tab.

 

First project

 

You’re now ready to start creating a project and connect it to your Salesforce org. You can do this by creating a new project in VSC. Use the command Ctrl + Shift + P and start writing Create Project with Manifest. First, you’ll need to specify what kind of project you want to create. For now, you can use the standard project (default). After that, you’ll need to specify the name of the project.

 

 

Now you need to connect this project with a dev org or a sandbox. You can also connect with a dev hub or scratch org, as described in the package-based development model.

 

 

After specifying the name of the org, it’ll automatically open the browser and the login page where you’ll need to log in with your org credentials.

 

 

It’ll ask you for all the permissions for the Salesforce CLI. Click ‘Allow’ and it’ll open your org.

After successfully authorising the org, you can now get all your metadata from your connected org. You can do this by going to the package.xml file under the manifest folder. Right click on the file, and click the option SFDX: Retrieve Source in Manifest from Org as shown here:

 

 

This package.xml file specifies all the metadata you want to retrieve from the specified org. Here’s more information on how to set up your own package.xml file: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/manifest_samples.htm

 

After all the metadata retrieved, you can view it in the force-app folder.

 

VSC extensions for Salesforce development

Apex PMD

The Apex PMD extension allows you to run Apex Static Analysis directly in VSC on Apex and Visualforce files.
https://marketplace.visualstudio.com/items?itemName=chuckjonas.apex-pmd

 

Prettier

We all love to have nice, formatted code so everyone’s able to read and understand the written developments. This extension is a code formatter and keeps your code consistent.
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

 

GitHub pull requests

This extension allows you to review and manage GitHub pull requests in VSC.
Check out what’s supported here.

 

Tips and tricks for using VSC

Deploy on Save

When you’re modifying a local file in VSC, you can enable immediate deployment of the changes to your configured org. This can be very useful if you want to test changes immediately. Deploy on Save can be enabled on a project level or globally.

Project level:
Add “salesforcedx-vscode-core.push-or-deploy-on-save.enabled“: true to the .vscode/settings.json file.

 

Global enablement:

  • Select File > Preferences > Settings (Windows or Linux) or Code > Preferences > Settings (macOS).
  • Under ‘Salesforce Feature Previews’, select Push-or-deploy-on-save: Enabled.

 

Open your org

You can open your org simply by clicking this highlighted icon in VSC:

 

Activate Demo mode

Want to demo your code at a conference? Set up Demo mode! When Demo mode’s enabled, VSC warns users who authorise business or production orgs of the potential security risks of using these orgs on shared machines.

Demo mode can be activated by adding the environment variable SFDX_ENV and setting the value to DEMO: SFDX_ENV=DEMO.

 

Check differences with org

Via ‘Diff File against org’ feature you see the differences in metadata against your configured org. This is extremely handy when you want to visualise the changes between your local file and the metadata in your org. Here’s a list of supported metadata: Apex Class, Apex Trigger, Aura Application, Aura Component, Aura Event, Aura Interface, Aura Token, LWC, Visualforce Page, Visualforce Component.

 

 

Resources

Want to learn more about VSC development? Here are some resources you can use as a starting point:

Quick Start: Visual Studio Code for Salesforce Development
Find and Fix Bugs with Apex Replay Debugger
Package Development Model
Org Development Model

Extensions Github

Extensions Documentation