Following are the steps to setup Salesforce on VS Code:
- Install VS code in your system using this link "https://code.visualstudio.com/download".
- Install Salesforce CLI using the link "https://developer.salesforce.com/tools/sfdxcli"
- Set the path of the bin folder of CLI in your system advanced variables.
Now you are ready to use VS code
Open the VS code terminal
1. Click on the extension icon as you need them to use various commands.
2. Search for salesforce extensions pack and install them.
3. Click on view and then command Palette.(Ctrl+Shift+P) or (Command+Shift+P) are the shortcuts for Windows and Mac respectively.
4. Type sfdx: Create a project with manifest - You are doing this to get a predefined package.xml to retrieve data from your salesforce org.
Don't forget to check out: Salesforce Source code management with Visual Studio code
5. Enter the name of the project.
6. Once you enter the name of the project, it will redirect you to choose any folder to save your project files.
The SFDX command will create you a project.
7. After the project is created, you can see the folders and files inside that project.click to Expand the folder ‘manifest’, and there you will see a package.xml. Click on that package.xml to retrieve the code from the connected org into VsCode.
8. Now you need to authorize an org to connect your org to VS.
9. Open the Command Palette and type sfdx: authorize an org followed by giving the alias for the org.
You will be asked to select login URL.
10. SFDX will run the command to run and it will bring you to the browser window to login to your salesforce org.
11. Enter the credentials and once login go back to your VS code.
12. To retrieve the metadata from your connected Salesforce Org, just right click on package.xml and click on ‘SFDX: Retrieve Source code from Org’.
13. After retrieved, you can see the metadata under the folder force-app-> main -> default.
14. Click to open any class or component you want to open. Code will be displayed in the VsCode editor.
15. You can start writing code. The error will be displayed in the "Problem" tab.
16. To deploy a single file to the organization, right-click the file name and select "SFDX: Deploy Source to Organization".
Check out another amazing blog by Anuj here: Schema Class in Salesforce Apex | The Developer Guide
17. To create a new component, activate the command panel (remember Shift + Command + P) and enter "SFDX: create". This will show you all the different components you can create. After completing the development, don't forget to run "SFDX: Deploy the source code to the org".
To search for files in the project, go to the command panel (Shift + Command + P), press the Backspace key to delete ">", and then type the search string. This command will show you all files in the current project that match your search string.
Run test class
To run the test course, click the "Test" icon in the VS Code. All test categories will be listed in the left column.
Click on the arrow in front of the test class name to expand the test class Click on the ‘Play’ icon to run the test class or particular methods within a class
The output result of the class will be displayed in the ‘Output’ panel
If you have SOQL queries in your code and want to see the output of the query, select the SOQL query, right-click and select command palette and type ‘SFDX: SOQL’ and select SFDX: Execute SOQL Query with Currently Selected Query. Then select API.
Note the command being executed in the Output panel
Execute the anonymous block
- Click File-> New File
- Select the language of "Apex".
- Write the code to be executed as an anonymous block, and then press Shift + Command + p to activate the command panel
- Type "anonymous" in the command panel, and then click "SFDX: execute anonymous Apex using the currently selected text"
The code will be executed and the detailed information will be displayed in the "Output" tab.