I recently started a new role as a Salesforce Developer at a small startup consultancy. For the first time, I had to set up Visual Studio Code for Salesforce. VS Code is the most appealing option due to its suite of Salesforce add-ons and SFDX integrations.
Here, I will explain how to connect VS Code to a Salesforce org step by step. Then, we will see how to create an Apex class to fetch data from the org, deploy the Apex class, and execute the Apex code in VS Code.
Connect VS Code to Salesforce Org
In the following steps, I explained how to set up VS Code and connect to the Salesforce org.
1. Download and Install VS Code
First, you need to download and install VS Code on your system. Here, you can see the different OSs available, and you need to download VS Code according to the OS installed on your system.

2. Download and Install Salesforce CLI
The Salesforce DX CLI (Command-Line Interface) is a tool for developers working with Salesforce. It allows you to interact with your Salesforce orgs, manage source code, and automate tasks via the command line.
You need to install Salesforce CLI on your system, and for that, you need to download the CLI according to the OS installed on your system.

Once you complete the CLI installation process, to check whether the CLI is installed in your system, go to the command prompt and enter ‘SFDX‘(Salesforce Developer Experience). You will get the CLI VERSION and other information if the CLI is successfully installed.

If the CLI isn’t installed, you will get an error message stating, ‘not recognized as an internal or external command. ‘

3. Install Salesforce Extension Pack for VS Code
After installing the CLI, we need to install the Salesforce Extension Pack in the VS code so that we can connect the VS code to the Salesforce org and access, create, and deploy apex, VF pages, and LWC to the org.
To install the extension pack, open the VS code that you installed. First, click the Extensions icon in the left sidebar or press Ctrl+Shift+X. Then, enter Salesforce Extension Pack in the search box and click the Install button.
With the Salesforce extension pack, all the extensions below will install automatically.
- Apex
- Apex Interactive Debugger
- Apex Replay Debugger
- Salesforce CLI Integration
- Visualforce
- Aura Components
- Lightning Web Component

4. Create a Salesforce Project and Folder
To create a project from a command, click the View menu and then select the Command Palette option. Alternatively, you can press Ctrl+Shift+P to open the command bar. In the command, you need to search for and select SFDX: Create Project With Manifest.
Then, you need to select Project Template. Here, I selected the Standard Template and press the Enter key.

After selecting the template, you have to enter the Project Name and press the enter key.

Now, we need to Select a Folder so that we can save our project. To do that, select an existing folder or create a new folder and click the Create Project button.

5. Connect VS Code to Salesforce Org
Now, we need to connect VS code to the Salesforce Org. For that, again, open the Command Palette. In the command, you need to search for and select SFDX: Authorize an Org.
Then, you need to select the default org that you are using. Here, I’m using Production org. If you have logged in through the URL to your org, then you can select Project Default or the Production org option.

After that, you will be redirected to the Salesforce Org login screen. Here, you need to enter credentials, or you will get a confirmation screen asking if you want to allow Salesforce Org to connect to VS Code. There, you need to click the Allow button.

After that, you will be connected to the Salesforce Org using VS Code and receive an Authentication Successful message.

6. Retrieve Component from Salesforce Org to the VS Code
Now, we need to fetch the apex classes and VF pages created in the Salesforce Developer Console. Navigate to the Package.xml file and open it in the code editor. Then, right-click on the opened code. A pop-up window will open, and in that window, select SFDX: Retrieve Source in Manifest from Org.

After that, you will see all the apex classes and VF pages that you have already created in the Salesforce org and be connected to them.

In this way, we can set up VS Code and connect it to the Salesforce Org.
Example: Retrieve Data From Salesforce Org to VS Code
Here, I will explain how to fetch the data after setting the connection between VS Code and the Salesforce Org. For that, I created an Apex program to fetch/ retrieve account object records from the Salesforce org and display them in VS Code.
In the following steps, I explained how to create an Apex class to fetch data from the org, how to deploy the Apex class, and how to execute the Apex code in VS Code.
1. Create an Apex Class in VS Code
1. To create an apex class, right-click on the classes component. A pop-up window will open, and you need to select the SFDX: Create Apex Class option.

OR, you can open the Command Palette by pressing the Ctrl+Shift+P keys. In the command, you need to search for and select SFDX: Create Apex Class.

Then, again, you will see a comment palette. There, you need to enter the Class Name.

The new class will be created with the name that you provided. Now, we want to fetch the account object records from the Salesforce org.
- getAccount(): In this method, we will perform an operation to fetch account records.
- List<Account>: The records we are fetching from the account object we will store in the list.
- SOQL Query: It will retrieve the records from the org.
- displayAccRecords(): This method executes for loop to display all records that we fetched from the getAccount() method.
After creating the code, Save it.

2. Deploy Apex Code From Source to Org
This is a very important step: after saving the code, we need to run it. Before executing the code, we need to deploy it to the Salesforce org. To do so, right-click on the code editor where the class file is opened, and then click SFDX: Deploy This Source to Org.
You must save and deploy the code to the org whenever you change it.
If the source gets successfully deployed, you will get a success message.

3. Run/Execute Apex Code in Salesforce
To execute the Apex code, navigate to the Scripts component in the Force App. As you open the scripts and then open the apex folder, you will see the hello.apex file that has already been created. Here, you need to open that apex file.
After opening the hello.apex file, we need to enter a statement to execute the apex code. We developed the displayAccRecords() method in apex code to display the fetched account records.
Below, I explained how to execute the apex code:
- Syntax to execute Static method: ClassName.MethodName.
FetchAccountRecords.DisplayAccRecords();- Syntax to execute the Non-Static method: In this, we need to create an object and instance of the class, and then we can execute the code.
ClassName a = new ClassName();
a.MethodName();
After that, to execute the code again, we need to open the command palette, then search and select SFDX: Execute Anonymous Apex with Editor Contents.

As you execute the code in the Output, you will see the result that we executed. Here, you can see we have successfully fetched the record from the account object.

In this way, we can fetch/Retrieve data from Salesforce Org and display it in the VS code editor.
Conclusion
In this tutorial, I explained what we need to connect VS Code to a Salesforce org. First, we need to download and install VS Code and Salesforce CLI. After installing both applications, we need to install the Salesforce extension pack. Then, I explained how to connect VS Code to Salesforce org step by step so that we can access Salesforce apex classes, VF pages, and triggers, and other Salesforce features from VS Code.
Additionally, I explained how to create an apex class, deploy it, and execute it in VS code so that we can fetch data from Salesforce Org to VS code.
You may like to read:
- Unable to Activate the Apex Language Server
- Create a Free Salesforce Developer org
- How to Find Salesforce Org ID?
- Create a New User Using Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.