Salesforce DX – Source Code Deployment using CLI and VS Code Extension

The Salesforce Developer Experience (DX) is a new set of tools that streamlines the entire development life cycle. It improves team development and collaboration, facilitates automated testing and continuous integration, and makes the release cycle more efficient and agile.

It provides an alternative to change set development, and shifts the source of truth from the org to your version control system (VCS). It shifts your development focus from org development to package development. Salesforce DX uses the migration tool from to retrieve and push metadata to an org, also known as a developer environment, but DX also pulls all functionality into one place.

Salesforce DX introduces a new type of Salesforce environment in Scratch orgs. These are orgs consisting of Salesforce code or metadata that can be easily created or destroyed, helping to speed up the standard development workflow.

There are few steps to setup Salesforce DX in your org.

Enable Dev Hub

We have to enable Dev Hub to create a Scratch Org. A Dev Hub provides you and your team with the ability to create and manage scratch orgs. Scratch orgs are temporary Salesforce environments where you do the bulk of your development work in this new source-driven development paradigm. Go to Setup > Find – Dev Hub > Enable

Screen Shot 2018-10-30 at 9.13.16 PM

Scratch Org

A scratch org is a dedicated, configurable, and short-term Salesforce environment.

Scratch orgs drive developer productivity and collaboration during the development process, and facilitate automated testing and continuous integration. You can use the CLI or Salesforce Extensions for VS Code to open your scratch org in a browser without logging in. You can spin up a new scratch org when you want to:

  • Start a new project.
  • Start a new feature branch.
  • Test a new feature.
  • Start automated testing.
  • Perform development tasks directly in an org.
  • Start from “scratch” with a fresh new org

Setup Salesforce CLI

We require Salesforce CLI (Command Line Interface) to execute commands for creating environments for development, testing , code sync and test executions etc. CLI also helps us to maintain the complete life-cycle of our apps.

Let’s start with the setup.

  1. Install Salesforce CLI from ( )
  2. Execute command  – sfdxsfdxCommand
  3. Authorize Dev Hub org using command :
    sfdx force:auth:web:login -d -a DevHub

    In the above command, DevHub is alias for the org. Alias helps us to login into the org when multiple orgs are opened using CLI.

  4. Once the above command is entered, the browser will be opened with SF login page.
  5. Once the login is successful. We can close the browser and below screen will come:
  6. We can reopen org anytime using using below command with alias :sfdx force:org:open -u DevHub
  7. For Sandbox login :
    sfdx force:auth:web:login r a FullSandbox

Setup Salesforce Extension for VS Code

This helps us to manage the code repository like version control system. We will discuss the use case in later example.

  1. Install Visual Studio Code:
  2. Install Salesforce Extension for VS Code

Create a New SF DX Project

We are going to create a new Project using CLI.

  1. Enter the below command to create new project under local directory C:\Users\Ajo\SalesforceDXDemo
    sfdx force:project:create -n SalesforceDXDemo

Once the project is created, we open the VS code to check the structure of our files and can sync comps from local directory to scratch org. Our components are stored in force-app folder.


Create Scratch Org using CLI

The Scratch Org will be used for development purpose.

Here are the steps to create a Scratch Org.

  1. In the command window, change to the geolocation project directory.
    cd geolocation
  2. Create a scratch org with the alias GeoAppScratch:
    sfdx force:org:create -s -f config/project-scratch-def.json -a GeoAppScratch
We can use the scratch org for our development purpose. It will create a brand new org which can be shown in the Dev Hub org.
Click on Tab named “Active Scratch Orgs” and check all the active scratch org like:

Create a Class in Scratch Org

  1. Open the Scratch Org
    sfdx force:org:open
  2. Create a class named “SalesforceDXDemoController”

public class SalesforceDXDemoController{
//Here is the code
String var = ‘Demo Value’;

public SalesforceDXDemoController(){
public void doCall(){

Pull Metadata (Class) from Scratch Org to Local

We use below command to pull the metadata:

( Go to the directory)
C:\Users\Ajo\SalesforceDXDemo>sfdx force:source:pull


Since we are are already logged into Scratch Org, it will pull the metadata to local directory in folder name “force-app”

path” C:\Users\Ajo\SalesforceDXDemo\force-app

We can see the newly created class in our VS code.


Push Metadata (Class) to Scratch Org

Now we will add one line code in the existing class and save to the local directory using VS code.  We have added line#7 in the existing code.


We use below command to push the metadata:
( Go to the directory)
C:\Users\Ajo\SalesforceDXDemo>sfdx force:source:push

This will deploy the class in scratch org and add the updated the metadata.

Note : If you want to push the metadata to the org with alias “DevHub” (Main org) , please reopen the org first. Then use ‘push’ command to deploy the changes to the DevHub org.  This works based on the recently opened org.

Retrieve Selective Metadata (Class) using package.xml

In the above examples, we have talked about pulling the metadata using command “force:source:pull” which pulls all the recent changes from local files changed by version control system tool (Extension for VS Code).

Now, we want to retrieve the selective classes mentioned in the package.xml file. We need to use the command mentioned below:

sfdx force:mdapi:retrieve -r ./DxDemoPkg  -u -k ./package.xml

It will retrieve the metadata from an org using metadata api. Here are the steps to get the metadata using package.xml:

  1. In the project, create a folder to store what’s retrieved from your org, for example, DxDemoPkg.
  2. Store the class “SalesforceDXDemoController” in package.xml.
  3. Retrieve the metadata.
  4. The username can be the scratch org username or an alias. The -k parameter indicates the path to the package.xml file, which is the unpackaged manifest of components to retrieve.
  5. Check the status of the retrieve.
  6. When you run force:mdapi:retrieve, the job ID, target username, and retrieve directory are stored, so you don’t have to specify these required parameters to check the status. These stored values are overwritten when you run the force:mdapi:retrieve again.
    If you want to check the status of a different retrieve operation, specify the retrieve directory and job ID on the command line, which overrides any stored values. Using below command:

          sfdx force:mdapi:retrieve -i jobID 

  1. Unzip the zip file.

After you retrieve the source and unzip it, you no longer need the zip file, so you can delete it.

After you finish, convert from metadata format to source format.



Leave a Reply


You are commenting using your account. Log Out /  Change )

Google photo


Twitter picture


Facebook photo


Connecting to %s