VS Code Salesforce Tutorial: Setup and Deployment Guide
Explore the integration of Salesforce and Visual Studio Code in this comprehensive guide. Learn how to download metadata, authorize your org, and save changes back to Salesforce.
May 23, 2023
July 26, 2021
In this article, we'll delve into how to get Visual Studio Code (VS Code), the Salesforce Command Line Interface (CLI), and the Salesforce Extension Pack running with your Salesforce sandboxes. We'll go step-by-step, explaining how to install the necessary tools, create a Salesforce project, authorize your Salesforce organization, and how to save changes back to Salesforce.
Our Key Takeaways are:
VS Code is a popular IDE among developers that supports multiple languages, including Salesforce.
The initial setup requires the installation of the Salesforce CLI, VS Code, and several Salesforce Extensions.
A Salesforce project can be created within VS Code, which is then connected to your Salesforce org.
You can download metadata locally from your org using the "Package.xml Generator: Choose Metadata Components" command.
Once changes are made in the files, you can save them and they will be deployed back to Salesforce, but this requires enabling the "Deploy on Save" setting.
Table of Contents
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.
Pushing your Salesforce Sandbox + VS Code experience further
Remember that the technology you use is as good as your understanding of it! Our goal with this guide here is to help you set up Visual Studio Code along with your Salesforce Sandboxes, and slowly but surely feel the benefits in terms of flow and productivity.
Adding to this set of tools, and in the same spirit of boosting your development workflow, we recommend you try Blue Canvas' Git-based deployment experience. You'll benefit from continuous Salesforce metadata backups, ability to compare orgs within seconds, and to cross-deploy metadata changes between orgs with team review and automated checks.
How do I use Salesforce with Visual Studio Code (VS Code)?
To use Salesforce with VS Code, you first need to install the Salesforce Command Line Interface (CLI) and then VS Code. Next, install the Salesforce Extensions within VS Code. After that, create a Salesforce project within VS Code and authorize your Salesforce organization. You can then download metadata locally and save changes back to Salesforce.
What are the prerequisites for running Salesforce with VS Code?
The initial setup requires the installation of the Salesforce CLI, VS Code, and several Salesforce Extensions. Once these tools are installed, you can create a Salesforce project within VS Code and authorize your Salesforce org.
Can I save changes made in VS Code back to Salesforce?
Yes, changes made in the files in VS Code can be saved and then deployed back to Salesforce. However, this requires enabling the "Deploy on Save" setting in your VS Code settings.
How can I download metadata locally from my Salesforce org using VS Code?
You can download metadata locally from your Salesforce org by using the "Package.xml Generator: Choose Metadata Components" command within VS Code. This will allow you to select the metadata items you wish to download.
What is the role of Salesforce Extensions in the VS Code Salesforce setup?
Salesforce Extensions play a crucial role in the VS Code Salesforce setup. These extensions, which include Salesforce Core Extensions and Salesforce Package.xml Generator Extension, provide functionality necessary for managing your Salesforce org within VS Code. They are regularly updated as new versions are released.
Can I use VS Code with a Salesforce sandbox?
Yes, VS Code supports Salesforce sandboxes. After setting up the necessary tools, you can authorize a Salesforce sandbox and start working with your Salesforce data within VS Code.