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 Force.com 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
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.
- Install Salesforce CLI from ( https://developer.salesforce.com/tools/sfdxcli )
- Execute command – sfdx
- 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.
- Once the above command is entered, the browser will be opened with SF login page.
- Once the login is successful. We can close the browser and below screen will come:
- We can reopen org anytime using using below command with alias :sfdx force:org:open -u DevHub
- For Sandbox login :
sfdx force:auth:web:login –r https://test.salesforce.com –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.
- Install Visual Studio Code:
https://code.visualstudio.com/ - Install Salesforce Extension for VS Code
https://marketplace.visualstudio.com/items?itemName=salesforce.salesforcedx-vscode
Create a New SF DX Project
We are going to create a new Project using CLI.
- 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.
- In the command window, change to the geolocation project directory.
- Create a scratch org with the alias GeoAppScratch:

Create a Class in Scratch Org
- Open the Scratch Org
sfdx force:org:open
- 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:
- In the project, create a folder to store what’s retrieved from your org, for example, DxDemoPkg.
- Store the class “SalesforceDXDemoController” in package.xml.
- Retrieve the metadata.
- 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.
- Check the status of the retrieve.
-
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
- 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.
References:
https://developer.salesforce.com/platform/dx
https://trailhead.salesforce.com/en/content/learn/projects/quick-start-salesforce-dx