Developer Tips
MISSING SUBTEXT

Running VS Code with a Salesforce Sandbox Using the Salesforce CLI

How to get VS Code and the Salesforce CLI and Extension Pack running with your sandboxes.

July 26, 2021

Overview

Visual Studio Code (VS Code) is an IDE used by many developers with support for multiple programming languages and tech stacks including Salesforce. VS Code replaced the Force.com IDE plugin for Eclipse about 2 years ago and initially only had support for scratch orgs. Now it supports sandboxes too, but Trailhead and other documentation don’t explain how to set it all up very well. Here are some simple instructions to get you up and running with VS Code, the Salesforce Extension Pack and your sandboxes.

Summary Steps

  1. Install Salesforce CLI, VS Code, & VS Code Salesforce Extensions - One time prerequisite
  2. Create the Salesforce Project with Manifest
  3. Authorize the Salesforce sandbox
  4. Download the desired metadata

Install CLI & VS Code

Before installing anything else, install the Salesforce Command Line Interface (CLI) and then VS Code.These install the necessary Salesforce DX items that the Salesforce extensions use under the hood in VS Code.

Install Salesforce Extensions

After the CLIand VS Code are installed, install these Salesforce extensions:

  1. Salesforce Core Extensions - These core extensions are published and maintained by Salesforce. They’re regularly updated as new versions are released.
  2. Salesforce Package.xml Generator Extension - This open source extension creates and updates the package.xml file and makes management easy.

Extensions installed? Great! This one-time setup is now complete. Complete the following steps for each org you’d like to use VS Code with.

Create Salesforce Project

Now you’re ready to create your Salesforce project in VS Code. Let’s do that by:

  1. Opening VS Code.
  2. Opening the command palette by going to View → Command Palette (Ctrl-Shift-P on Windows and Cmd-Shift-P on Mac).
  3. Start typing create and click “Create Project with Manifest”:


  1. Choose Empty for an empty template. This is recommended to make initial setup faster since you’ll be using another extension to choose the metadata to download later.
  2. For the project name, be as qualified and concise as possible and include the org name. For sandboxes, the sandbox name is usually enough. If working in multiple customer orgs, prefix it with the customers initials followed by the sandbox name. For example, “BC SIT” could be used for the Blue Canvas SIT sandbox. 
  3. Hit enter. VS Code prompts for the folder where the project will be stored. Create new folders as needed to keep organized and make it easy to find projects later!
  4. Click Create Project. VS Code now creates folders and other default files.

Authorize an Org

Now that the project files and folders are created, let’s connect VS Code with your Salesforce org using the “Authorize an Org” command. This involves logging into your org and authorizing VS Code to connect to your org:

  1. Open the command palette.
  2. Start typing Auth and select “SFDX: Authorize an Org”.(Note: Sometimes the command doesn’t show immediately because the extensions take a bit to load.)
  1. Now the different org type options are shown. Choose Sandbox to connect to a sandbox. Use Production to connect to your production org or non-sandbox developer edition org.
  2. Next you’re prompted for the VS Code Org name. Try to give it the same name as your project if possible and use the same naming guidelines. After a selection is made, your browser opens with the appropriate Salesforce login page.
  1. Enter the username and password and click Login. On success, you may be prompted to “Allow” VS Code to connect to your org. Click Allow. That doesn’t show if you’ve previously allowed it. On success, VS Code will show this toast message at the bottom right:

 

Download the Metadata

VS Code is now connected to your org and ready to download the metadata locally. First let’s choose the metadata to download:


  1. Open the command palette.
  2. Start typing “Package” and then select “SFDX: Package.xml Generator: Choose Metadata Components”.
  1. This opens a new “Choose Metadata Components” tab and the extension starts fetching the list of available metadata from your org.
  2. Now select the metadata items to download. These items are recommended at a minimum since they’re the most common developer ones:
  1. Apex Class
  2. ApexComponent
  3. ApexPage
  4. ApexTrigger
  5. AuraDefinitionBundle
  6. CustomLabels
  7. CustomMetadata
  8. CustomObject - Actually picks all standard and custom Objects
  9. Flow
  10. LightningComponentBundle
  11. Profile
  12. PermissionSet
  13. Static Resource
  1. Click “Update Package.xml”. This updates the Package.xml file.
  2. Right-click the “Package.xml” file and select “SFDX: Retrieve Source in Manifest from Org”.
  1. Now VS Code is downloading the selected metadata. This can take a while but usually finishes within 5-10 minutes depending on how much has to be downloaded.

Save Changes To Salesforce

To deploy changes back to Salesforce, one can simply save the file and then they’re deployed to the org. However, the Deploy on Save setting needs to be updated first:

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

Or update Workspace settings:

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

Alternatively, one can right-click on the changed file(s) and select SFDX: Deploy Source to Server. This is a fall-back option and saving to the org on file save is preferred.


More like this