Salesforce DX means Salesforce Development Experience. SFDX is a modern tool used for building Salesforce lightning applications. Using Salesforce Dx, Salesforce developers can keep the source code in a version control system such as GitHub, Team Foundation Server (TFS), Subversion (SVN). Then the Salesforce developers can deploy the source code into a Salesforce instance, referred to as scratch Orgs, using commands provided by the Salesforce DX CLI. Salesforce DX allows users to create scratch Orgs, where developers can enhance the source code of their applications and test them.
What is Salesforce DX CLI?
Salesforce CLI is a powerful Command Line Interface that provides commands to push the source code from your machine (referred to as local) to a Salesforce instance, create a Lightning Component Bundle, test Lightning Components, and many others.
In this Salesforce Lightning Tutorial, we will learn the following
- Enabling the developer hub in our Salesforce organization.
- Installing the Salesforce DX CLI
- List of Salesforce DX commands.
- Installing the Visual Studio extension pack for DX.
Enabling Developer Hub in Salesforce Organization.
Salesforce DX users create scratch Orgs, where developers can enhance the source code of their applications and test them. Scratch Org creation is possible only the Developer Hub in Salesforce organization is enabled. The Dev Hub allows us to manage the scratch Orgs created by your developers. With scratch Orgs, each developer can build an application separately in their own scratch Org without stepping on each other’s code. To enable Developer Hub to follow the steps shown below:
- Log in to your production Org.
- From Setup, enter Dev Hub in the Quick Find box and select Dev Hub.
- To enable the Dev Hub, click Enable.
- After you have enabled the Dev Hub, you can’t disable it. If you’re using a trial Org, Dev Hub is already enabled.
Installing Salesforce DX CLI
Salesforce DX CLI enables users to Create and manage Scratch Orgs, to Push and pull code and configuration from source control to Salesforce scratch Orgs, to Load sample datasets into scratch Orgs, to Assign Permission sets, to Install and uninstall managed package applications from the command line.
How to download Salesforce DX CLI?
To install the CLI, visit the Salesforce CLI website at https://developer.salesforce.com/tools/sfdxcli. Depending on your operating system, you can download the executable.
Once your installation is done, make sure to type the sfdx command in your terminal to ensure that the Salesforce DX CLI is installed. The following terminal shows that the CLI is installed successfully.
Last login: Thu Jun 3 09:25:15 on console prasanthkumar@Prasanths-MacBook-Pro ~ % sfdx Salesforce CLI VERSION sfdx-cli/7.81.0-7b953c80d1 darwin-x64 node-v12.18.3 USAGE $ sfdx [COMMAND] TOPICS alias manage username aliases auth authorize an org for use with the Salesforce CLI config configure the Salesforce CLI force tools for the Salesforce developer plugins add/remove/create CLI plug-ins schema List metadata types in your Salesforce org using the CLI COMMANDS autocomplete display autocomplete installation instructions commands list all the commands help display help for sfdx plugins list installed plugins update update the sfdx CLI which show which plugin a command is in prasanthkumar@Prasanths-MacBook-Pro ~ %
Salesforce DX Commands
In this section, we will cover some basic commands to authorize Salesforce, set a default Dev Hub username, and scaffold a Salesforce DX application. To get the list of Salesforce DX commands enter the following command
prasanthkumar@Prasanths-MacBook-Pro ~ % sfdx commands
auth commands
The first thing we need to do is to connect the Salesforce DX CLI to Salesforce. The CLI provides commands to connect using web-based OAuth flow, JWT token-based flow, and an sfdx auth URL. To connect the Dev Hub to the CLI, run the following command:
sfdx force:auth:web:login -r https://login.Salesforce.com
For Sandbox Orgs, use test.Salesforce.com instead. The following screenshot shows the Terminal once the CLI has been authorized to connect to Salesforce.
prasanthkumar@Prasanths-MacBook-Pro ~ % sfdx force:auth:web:login -r https://login.Salesforce.com Successfully authorized prasanth.developer@tutorialkart.com with org ID 00D2x0000000000O You may now close the browser prasanthkumar@Prasanths-MacBook-Pro ~ %
Setting a default Dev Hub for scratch Org creation
To configure the default Dev Hub globally, execute the following command:
prasanthkumar@Prasanths-MacBook-Pro ~ % sfdx force:config:set defaultdevhubusername=prasanth.developer@tutorialkart.com -g === Set Config Name Value Success ───────────────────── ─────────────────────────────────── ─────── defaultdevhubusername prasanth.developer@tutorialkart.com true prasanthkumar@Prasanths-MacBook-Pro ~ %
Here, -g denotes global, and hence, by default, for all the DX projects, the same Dev Hub username will be used to create a scratch Org for other DX projects. If you want to set a different Dev Hub, then omit -g and execute the same command in your project directory.
Creating new Salesforce DX project
To create a new DX project, run the following command in the directory that you want the new project to reside in:
sfdx force:project:create -n SampleDXProject
Configuring a scratch Org definition JSON
The configuration in the project-scratch-def.json file defines the structure of the scratch Org. Let’s look at the following sample project-scratch-def.json file:
{ "OrgName": "Salesforce", "country": "India", "edition": "Enterprise", "features": "MultiCurrency;AuthorApex", "OrgPreferences": { "enabled": ["S1DesktopEnabled", "ChatterEnabled"], "disabled": ["SelfSetPasswordInApi"] } }
In the preceding file, we define editions and features we want to enable. It also shows that the scratch Org that we create here will be of Enterprise edition with Salesforce features such as MultiCurrency and AuthorApex enabled. Also, the Salesforce scratch organization will be created, which will have chatter and Salesforce1 enabled, but SelfSetPasswordInAPI will not be enabled.
Configuring project definition JSON
The sfdx-project.json file enables the configuration parameter to set sourceAPIVersion and namespace if you are building a managed package application. A sample sfdx-project.json file is as follows:
{ "packageDirectories": [{ "path": "force-app", "default": true }], "namespace": "", "sfdcLoginUrl": "https://login.Salesforce.com", "sourceApiVersion": "51.0" }
packageDirectories is the location of the directory to which the source from the scratch Org (managed package, unmanaged package, or unpacked code) will be pulled and pushed. We can specify multiple paths, but only one path can be the default. The following code shows an example of how to specify multiple paths:
"packageDirectories" : [ { "path": "force-app", "default": true}, { "path" : "unpackaged" }, { "path" : "utils" } ], "namespace": "", "sfdcLoginUrl" : "https://login.Salesforce.com", "sourceApiVersion": "51.0" }
Creating a Scratch Org
Scratch Orgin Salesforce lightning is the Org where developers will build source code and cinfigure an application. To create a scratch Org from the command line , execute the following command
sfdx force:org:create -f project-scratch-def.json -a LighntningScratchOrg
LightningScratchOrg is the alias of the Org.
Openig the Scratch Org using Command Line
Before opening the Scratch Org, we must set the default Org for pushing and pulling the source. Use the command line to set default Scratc Org using CLI
sfdx force:config:set defaultusername=<username>
To open a scratch Org, use the following command:
sfdx force:org:open -u <username/alias>
How to pull source from a Scratch Org
To pull source code component from a scratch Org, use
sfdx force:source:pull
Now we will create a simple component bundle called HelloPrasanth in the scratch Org and retrieve into the local folder using the command line. To open the Org from the command line, use sfdx force:org open -u <username>.Use the Developer console to create helloPrasanth component, as shown below.