Set Up Salesforce DX
- Describe how the model for traditional org development differs from modular package development.
- Describe the key characteristics of a package.
Get Started with Source-Driven Development
Salesforce DX adds new tools that streamline 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.
But Salesforce DX is so much more than just a new set of tools! 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. To learn how to migrate your existing dev processes to the package development model, head over to the Package Development Model module.
But enough with the chit-chat. Are you ready to start getting hands-on? Let’s get started setting up your environment and introducing you to some new and some familiar tools.
What’s a Scratch Org?
Many of the new tools enable you to use a new type of org called a 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.
Although scratch orgs are meant to be disposable, the scratch org configuration files contain the real brawn. Through the configuration file, you can configure the scratch org with different Salesforce editions and with just the features and settings you want. And you can share the scratch org configuration file with other team members. That way, you all have the same basic org in which to do your development.
Do Scratch Orgs Replace Sandboxes?
No. Scratch orgs aren’t meant to be replications of sandboxes or production orgs. Due to their ephemeral nature (and maximum 30-day lifespan), scratch orgs are perfect for developing a new feature or customization or package. And they work great for unit testing and continuous integration. Sandboxes, which contain all the metadata of your production org, are still necessary for final user-acceptance testing, continuous delivery, and staging.
So far, so good? Read on...
Enable Dev Hub
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.
To get started with scratch orgs, you choose an org to function as your Dev Hub. While you can enable Dev Hub in any paid org, it’s always best to practice somewhere other than production. Instead, go ahead and enable Dev Hub in a Developer Edition org or Trailhead Playground to use with this module.
You can also make any paid org your Dev Hub and grant access to developers. Get the details in the Salesforce CLI Setup Guide.
Now that you have a Dev Hub org, let’s set up the rest of your Salesforce DX tools.
Install the Command Line Interface (CLI)
- Install the CLI from https://developer.salesforce.com/tools/sfdxcli.
- Confirm the CLI is properly installed and on the latest version by running the following command from the command line.
sfdx updateYou should see output like
sfdx-cli: Updating CLI....
Here are some other helpful commands to get you started:
|Command||What You See|
||All the available topics in the
||All available commands|
OK, you’re well on your way. Now let’s continue installing the rest of the Salesforce DX tooling.
Log In to the Dev Hub
To get started, log in to the Dev Hub using the CLI, so you’re authorized to create scratch orgs. You can use
sfdx auth:web:login to log in to various orgs, and we’ve provided some options to help you manage those orgs.
sfdx auth:web:login -h sfdx auth:web:login --help
- To authorize the Dev Hub, use the web login flow:
sfdx auth:web:login -d -a DevHubAdding the
-dflag sets this org as the default Dev Hub. Use the
-aflag to set an alias for the org (something catchy like DevHub). An alias is much easier to remember than the unique Dev Hub username. Important: Only indicate the
-dflag for your Dev Hub. If you use it with a different org, you can’t create scratch orgs until you correctly identify the Dev Hub using the
- Log in with your credentials.
Once successful, the CLI securely stores the token along with the alias for the org, in this example, DevHub. You can close the Dev Hub org at any time.
You can close the Dev Hub and still create scratch orgs. However, if you want to open the Dev Hub org to look at active scratch orgs or your namespace registry, the alias comes in quite handy:
sfdx force:org:open -u DevHub
A Bit More on Org Management
It’s likely you have many orgs, including sandboxes and your production org. With the CLI, you can also log in to them using these commands. When you log in to an org using the CLI, you add that org to the list of orgs that the CLI can work with in the future.
Log In to Sandboxes
If you create an alias for the sandbox (
-a flag), you can reference it by this alias instead of its long and often unintuitive username.
sfdx auth:web:login -r https://test.salesforce.com -a FullSandbox sfdx auth:web:login -r https://test.salesforce.com -a DevSandbox
The Power of Aliasing
As you might imagine, aliasing is a powerful way to manage and track your orgs, and we consider it a best practice. Why? Let’s look at scratch org usernames as an example. A scratch org username looks something like
email@example.com. Not easy to remember. So when you issue a command that requires the org username, using an alias for the org that you can remember can speed things up.
sfdx force:org:open -u FullSandbox sfdx force:org:open -u MyScratchOrg sfdx force:limits:api:display -u DevSandbox
View All Orgs
At any point, you can run the command
sfdx force:org:list to see all the orgs you’ve logged in to. Adding the
--verbose option provides you even more info.
Now you’re really ready to get going—let’s go build a new app with the Salesforce CLI and scratch orgs.