If you are serious about Salesforce development, moving away from the in-browser Developer Console to a local development environment is a must. Visual Studio Code (VS Code) offers a massive ecosystem of plugins, powerful syntax highlighting, and seamless Git version control integration.
In this guide, we will walk through the exact steps to configure VS Code for modern Salesforce DX (SFDX) development so you can start writing code efficiently.
Prerequisites
Before touching any code, you need two fundamental tools installed on your computer:
- Salesforce CLI: This is the command-line interface that allows your computer to talk to your Salesforce org. Download the latest installer from the official Salesforce developer website. (Note: Salesforce is actively transitioning from the older
sfdxcommands to the newer, fastersfcommands). - Visual Studio Code: Download and install the latest stable build of VS Code for your operating system (Windows, Mac, or Linux).
Step 1: Install the Salesforce Extension Pack
Out of the box, VS Code doesn't know what Apex or LWC is. We need to teach it by installing Salesforce's official extensions.
- Open VS Code.
- Click on the Extensions icon in the left sidebar (or press
Ctrl+Shift+Xon Windows /Cmd+Shift+Xon Mac). - Type Salesforce Extension Pack into the search bar.
- Click Install on the official pack provided by Salesforce.
Note: This single pack automatically installs everything you need, including Apex syntax highlighters, LWC support, SOQL builders, and Visualforce tools.
Step 2: Create a Salesforce DX Project
You cannot just open an empty folder and start coding. Salesforce requires a specific folder structure (an SFDX Project) to know where to place classes, components, and configuration files.
1. Open the VS Code Command Palette by pressing
Ctrl+Shift+P (Windows) or Cmd+Shift+P (Mac).2. Type
SFDX: Create Project and hit Enter.3. Select the Standard project template.
4. Give your project a name (e.g.,
MyFirstSalesforceApp).5. Choose a folder on your computer to save the project.
Step 3: Authenticate with Your Salesforce Org
Now that your project is set up locally, you need to connect it to your target Salesforce environment (Sandbox, Developer Edition, or Production).
- Open the Command Palette (
Ctrl+Shift+P). - Type and select
SFDX: Authorize an Org. - Select your login URL (choose Production for standard Developer Edition orgs, or Sandbox for testing environments).
- Enter an alias for this org (e.g.,
DevSandbox) so you can remember it later. - Your default web browser will open. Log in with your Salesforce credentials and click Allow when prompted for CLI access.
A very common mistake for beginners is trying to authorize a Sandbox while leaving the login URL set to login.salesforce.com. Sandboxes must always be routed through test.salesforce.com. If your authentication keeps failing, double-check your environment selection!
Step 4: Working with SFDX Commands
With your org connected, you are ready to write and deploy code. You have two main ways to interact with Salesforce from VS Code:
- The Command Palette: Use
Ctrl+Shift+Pfor point-and-click commands. For example, you can right-click any Apex class in your sidebar and select "SFDX: Deploy Source to Org" to push your changes to the cloud. - The Integrated Terminal: For faster, more advanced control, open the VS Code terminal (
Ctrl+`) and type modernsfCLI commands directly. For instance,sf project deploy startdeploys your entire project at once.
Step 5: Enhance Development with Extra Extensions
While the official Salesforce pack is mandatory, seasoned developers install a few extra community plugins to make life easier:
- Prettier - Code Formatter: Automatically formats your HTML, CSS, and JavaScript so your code always looks clean and uniform.
- Salesforce Package.xml Generator: A lifesaver for non-scratch org development. It provides a visual UI to select which metadata (like custom objects, fields, and layouts) you want to pull down from your org into VS Code.
1. Install the Salesforce CLI (
sf command line).2. Install Visual Studio Code.
3. Add the "Salesforce Extension Pack" via the VS Code Marketplace.
4. Run
SFDX: Create Project to scaffold your folders.5. Run
SFDX: Authorize an Org to connect to the cloud.
Conclusion
Setting up Visual Studio Code for Salesforce development transforms the way you write code. By migrating away from the browser and embracing local SFDX development, you gain access to powerful debugging tools, syntax checking, and version control. Follow these steps, explore the Command Palette, and you will be deploying enterprise-grade Lightning Web Components in no time.
Happy coding!