3-minute read

Overview

This tutorial will walk you through setting up a Salesforce developer environment with Salesforce DX, Visual Studio Code, and Git. Hopefully by the end you will see the benefits of working with these tools to make your Salesforce development life much more efficient.

Prerequisites

First you will need to download and install the following tools.

Note: This tutorial will be using Windows 10.

Setting up Visual Studio Code

Visual Studio Code is a great code editor and comes with lots of useful things out-of-the-box. However, to work with Salesforce, we will need to install the “Salesforce Extension Pack.” Open up Visual Studio Code and go to Extensions tab and search for “Salesforce Extension Pack.” Download and install. This will download several extensions which will integrate Salesforce with Visual Studio Code.

After installing and enabling the Salesforce extensions, we need to configure the Salesforce extension to use Java.

    • Go to Settings (File -> Preferences -> Settings).
    • On the User tab, expand Extensions and select Salesforce Apex Configuration.
    • Look for Java:Home setting.
    • 4 Enter the location of where your Java is installed (For example, C:\\Program Files\\Java\\jdk-11.0.6).

Doing this will enable you to utilize certain Java functions such as “Go to Definition” which when right-clicking on a function call, will forward you to the function defined. It will also enable auto-completion functionality. These are nifty features that will make development life much better.

Setting up a Salesforce project

To begin Salesforce development, we need to create a Salesforce project in Visual Studio Code.

      1. Open Command Palette (Ctrl + Shift + P) or under View -> Command Palette.
      2. You now have two options of creating a project:
        • SFDX: Create Project | For developing using scratch orgs where deploys as a single package.
        • SFDX: Create Project with Manifest | Allows for downloading and deploying all code and metadata directly to Salesforce org.
      3. We will use SFDX: Create Project with Manifest so we can develop directly with the Salesforce org.
      4. Choose Standard Project.
      5. Enter a name for your project.
      6. Choose the location in your file system to save your project in.
      7. The project will be created. Here are some notable files and folders:
      8. force-app/main/default
      9. This will be where your source code and metadata for your Salesforce org will be located. (There won’t be any files in the folders for now).
      10. sfdx-project.json
      11. This will be your project configuration where you can add or change location of where your source code will be saved.
      12. manifest/package.xml
      13. This will be used to retrieve source code and metadata.
      14. Now we have project set up, we need to connect to a Salesforce org.
      15. Open Command Palette and enter SFDX: Set a Default Org
      16. Select SFDX: Authorize an Org
      17. Select the login URL for your Salesforce org. We will use Sandbox.
      18. Enter an alias for your org. Press Enter key and browser will open where you can login to your Salesforce org.
      19. Login to your Salesforce org. (Note: there will be a prompt asking for Salesforce CLI access, allow this to continue).
      20. Once logged in, go back to Visual Studio Code and you should see a “Successfully authorized” message and you may close the browser.
      21. Notice that your Org alias name now appears on the bottom bar of Visual Studio Code. You will also see the alias when you run SFDC: Set a Default Org, this allows you to freely swap between different orgs.

Now you have integrated your Salesforce Org with your Salesforce project.

Retrieving/Deploying source code and metadata

Now that you have integrated your Salesforce org with the project. You can download the source code and metadata. Open up Command Palette again and run SFDX: Retrieve Source in Manifest from Org. This will download all the metadata from your Salesforce org. The contents of the metadata to retrieve is defined in the manifest/package.xml under your project folder. You can modify the package.xml to your liking.

All the source code will be downloaded to path set on the sfdx-project.json file under packageDirectories. Default will be “force-app/main/default” folder.

To deploy your code to your org, right-click on a file on the Explorer view and select “SFDX: Deploy Source to Org”.

To retrieve the latest code from your org, right-click on the file on the Explorer view and select “SFDX: Retrieve Source from Org”.

Be careful of overwriting other team member’s code as the deploy will overwrite. To make sure this doesn’t happen, make sure to retrieve first and then deploy. Another way to check is using “SFDX: Diff File Against Org” which will compare your local code with what is in the org. This command doesn’t come out-of-the-box; you will need to install it by running this command in command line or terminal: “sfdx plugins:install @salesforce/sfdx-diff”.

Working with Git

If your Salesforce code and metadata files are stored in Git, here is a way to utilize Git with SFDX in Visual Studio Code. We will be cloning the Git project into your sfdx package folder (force-app/main/default). Doing this will allow you to develop your code and check what changes were made compared to Git and also when retrieving/deploying code in the same file.

What’s next

Explore more of the SFDX functionality as there are a bunch of nifty features. Visual Studio Code integrates pretty well with SFDX. For example, you can execute code anonymously or run apex tests in Visual Studio Code.

Like what you see?

Paul Lee

Canh Sy is a Lead Developer with experience working with various technologies focused on back-end development and integrating with cloud computing platforms such as Amazon Web Services (AWS) and Salesforce.

Author