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
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.
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:
Opening VS Code.
Opening the command palette by going to View → Command Palette (Ctrl-Shift-P on Windows and Cmd-Shift-P on Mac).
Start typing create and click “Create Project with Manifest”:
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.
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.
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!
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:
Open the command palette.
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.)
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.
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.
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:
Open the command palette.
Start typing “Package” and then select “SFDX: Package.xml Generator: Choose Metadata Components”.
This opens a new “Choose Metadata Components” tab and the extension starts fetching the list of available metadata from your org.
Now select the metadata items to download. These items are recommended at a minimum since they’re the most common developer ones:
CustomObject - Actually picks all standard and custom Objects
Click “Update Package.xml”. This updates the Package.xml file.
Right-click the “Package.xml” file and select “SFDX: Retrieve Source in Manifest from Org”.
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.