Skip to main content

Set Up Salesforce DX Tools

Learning Objectives

After completing this unit, you’ll be able to:
  • 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 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 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 a few tools.

What’s a Scratch Org?

Many of the Salesforce DX tools enable you to use a 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 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, enable Dev Hub in a Developer Edition org or Trailhead Playground to use with this module. You can find your existing Trailhead playgrounds or create a new one by clicking your profile picture, then selecting Hands-On Orgs.

  1. Log into the org you’ve chosen as your Dev Hub.
  2. In Setup, enter Dev Hub in the Quick Find box and select Dev Hub.
  3. Click the button to enable Dev Hub.

You can also make any paid org your Dev Hub and grant access to developers. Get the details in the Salesforce DX Developer Guide.

Now that you have a Dev Hub org, let’s set up the rest of your Salesforce DX tools.

Install Salesforce CLI

Use the Salesforce CLI to control the full application life cycle of your apps. You can easily create environments for development and testing, synchronize source code between your orgs and VCS, and execute tests.
Note

See the Salesforce CLI Setup Guide for complete installation instructions.

  1. Install the CLI from https://developer.salesforce.com/tools/salesforcecli.
  2. Confirm the CLI is properly installed and on the latest version by running the following command from the command line.
    sf update
    You should see output like @salesforce/cli: Updating CLI.... The update command then displays the release notes for the latest version.

Here are some other helpful commands to get you started:

Command What You See
sf --help All the available topics  and the top-level commands.
sf commands All available commands.
You can display help for any command to get more information. The -h flag provides concise information about what the command and its flags do. The --help flag provides detailed descriptions and examples. Here's how to display both types of help for a command you'll soon run: org login web.
sf org login web -h
sf org login web --help

Many CLI command flags have both a long and short name. For example, many commands use --target-org to specify an org; it has the short name -o.  For clarity, we show only long flag names in this module,  but feel free to use the short name if you want. 

Now let’s continue setting up 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 the org login web command to log in to various orgs, and we’ve provided some options to help you manage those orgs.

  1. To authorize the Dev Hub, use the web login flow:
    sf org login web --set-default-dev-hub --alias DevHub
    Specifying the --set-default-dev-hub flag sets this org as the default Dev Hub. Use the --alias flag 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 --set-default-dev-hub flag 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 config set command.

  2. Log in with your credentials in the browser window that opens.

    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 browser window 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:

sf org open --target-org 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 ( --alias flag), you can reference it by this alias instead of its long and often unintuitive username.

For example:

sf org login web --instance-url https://MyDomainName--SandboxName.my.salesforce.com --alias FullSandbox
sf org login web --instance-url https://MyDomainName--SandboxName.my.salesforce.com --alias DevSandbox

If you don't know your org's login URL, you can find it on the My Domain Setup Page.


Note

Remember, don’t use the --set-default-dev-hub flag when logging into a sandbox. If you do, the CLI thinks the org is your Dev Hub, and then you’ll see an error when you try to create a scratch org.

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 test-7emx29rtpx0y@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.

sf org open --target-org FullSandbox
sf org open --target-org MyScratchOrg
sf limits api display --target-org DevSandbox

View All Orgs

At any point, you can run the command sf org list to see all the orgs you’ve logged in to. Adding the --all flag provides you even more info, such as recently deleted scratch orgs.

Now you’re really ready to get going—let’s go build a new app with the Salesforce CLI and scratch orgs.

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities