Data Table

Use VSCode for Salesforce Development

Topics

This post covers following topics below:

  • Create Project for Salesforce Development.
  • Authorise an org.
  • Retrieve all metadata from Salesforce to local.
  • Deploy changes in a file from local to Salesforce.
  • Create Salesforce Files.
  • Use saved alias.

Before you Start

  • Download and install Visual Studio Code, Salesforce Extensions Packs, and Salesforce Command Line Interface (CLI).
  • If you have not installed tools above, you can follow my other post in this link

1. Create a Project with manifest

  • Press Ctrl + Shift + P on Windows or Command + Shift + P on Mac to open up command pallete.
  • Type “sfdx:Create Project“, it has IntelliSense that displays options to choose. Then, choose “sfdx:Create Project with Manifest”
project manifest
  • It prompts a textfield to enter project name – put any project name
  • Example: vscodeTut or something else, then press enter. After that, command pallette will close
VSCode Project Name

After command pallete has closed, it refreshes VSCode and reopen the project again with additional folders like image below:

vscode file structure

main folder

It has default folder. Inside default folder, it has salesforce core folders below:

  • classes folder – it contains all apex files.
  • triggers folder – it contains all trigger files
  • aura folder – it contains all Aura Component files
  • lwc folder – it contains all Lightning Web Component related files
Salesforce File Structure

2. Authorise an Org

  • Press ctrl + shift + p, after that type “sfdx:authorize”.
  • Choose “SFDX: Authorize an Org”
Authorize an Org
  • It prompts command pallete to choose type of org. 
  • Accept default login URL
Org Type
  • Enter alias name such as – vsCodeDev
  • It prompts a web browser that navigate to Salesforce login page. Then, put existing user’s credential
Allow Access
  • Close the web browser
  • Open up Visual Studio Code, look at the command prompt.
  • It displays that “Authorize has successful” at Output tab
Successful Authorise

3. Retrieve All Metadata from Saleseforce

manifest folder

  • Expand manifest folder and select package.xml. 
  • Right-click and choose an option “SFDX: Retrieve Source in Manifest from org”
Retrieve Source
Success Retrieve

4. Deploy a Single File to Salesforce

After finish makes some changes in the file and would like to deploy it to Salesforce.

1. Save the file in VSCode.
2. Then right-click and choose “Deploy this Source to Org”.
Deploy Source

If successful, it displays the result like image below in Vscode Output tab.

SF Success Deploy

Create Salesforce Files

VSCode can create any type Salesforce files. However, this post will cover most common ones because the processes are the same.
1. Apex file

  • Right-click classes folder > choose sfdx:Create Apex class.
Apex Class
  • Put Apex class name in prompted command pallete.
apex classname file
  • Choose folder in prompted command pallete. It usually has pick the correct folder, hence, just press enter to put into that folder.
Apex Directory
  • If successful, it creates apex class with predefined apex class template as well.
Opportunity Example

2. trigger file

  • Select triggers folder > right-click > choose “Create Apex Trigger” option.
Trigger Example
  • Put trigger name in prompted command pallete.
  • Press “Enter” in prompted folder location.
  • VSCode creates trigger file and insert predefined trigger template.
Trigger Result

3. Aura components

  • Select aura folder > right click > choose “SFDX: Create Lightning Component”
lightning component
  • Put Aura component name in prompted command pallete.
  • Press Enter in prompted folder location.
  • VSCode creates folder and its related files.
Contact List

Handy Tips

Re-authorize org using Alias

If we are away from the computer. Occasionally, Salesforce requires to re-authorize the org again. However, there is a shortcut for that process by following steps below:

1. Select an icon at the bottom of the page
shortcut
2. It prompts the command palette with a list of an alias that has made locally. Choose the one that you want to use. Example below, choose cwpack1
authorize

Leave a Reply