Creating a Dev Hub and a Scratch Org for Trailhead Practice

ליאור נכתב על ידי ליאור לביא, עודכן בתאריך 03/11/2023

What Are Devhub and Scratch Org and How Do They Help Us

DevHub is a feature of a Production or Trailhead environment that can be activated to create scratch orgs. Once this feature is activated, it cannot be turned off.

Scratch org is a special type of Salesforce environment that can be created and deleted quickly. How fast? Creating a scratch org can take less than a minute, and deleting it is a matter of seconds. What makes scratch orgs so useful for developers is the fact that they are created completely empty of custom data and metadata, which means they are entirely clean environments where development can be done without worrying about custom data or metadata from a specific customer environment.

Additionally, you can easily configure environment types (Unlimited, Enterprise, Developer, and more) and specific features for scratch orgs, such as support for certain languages like End-User languages or installing packages like Service Cloud and features like Knowledge Articles, if our development relies on them.

One last point regarding scratch orgs is the ability to create them through terminal commands, allowing them to be smoothly integrated into the CI/CD process for DevOps, for testing before approving pull requests in the Develop and Master branches in Git.

Due to the convenience and ease of creating scratch orgs, they are especially handy for creating practice code examples like in this blog. Creating our DevHub will enable us to easily create a scratch org for practice using just one command in the terminal and simplify our learning.

Creating a Trailhead Playground

First, let's start by creating a Trailhead playground environment that will serve as our Dev Hub and allow us to create Scratch orgs:

  1. Log in to Trailhead. If you don't have a Trailhead account, click here for a guide on creating a Trailhead account and your first Playground environment.
  2. Click on your name in the top right corner of the screen, next to your profile picture, and from the menu, select Hands-On Orgs.
  3. Click the Create Playground button.
  4. Assign a name to the new environment, for example, My DevHub, and click Create.
  5. Wait for Salesforce to finish creating the environment for you. This may take a few minutes. My DevHub
  6. Once the environment is created, click the Launch button next to the environment name to open it.
  7. After the environment is opened, click on the Get Your Login Credentials tab.
  8. In the Get Your Login Credentials tab, copy your username and save it on the side; we will need it later.
  9. Click the Reset My Password button. In the window that opens, click OK.
  10. Open your email inbox, the one you registered with Trailhead, and open the email with the subject Finish resetting your Your Developer Edition password.
  11. Click the link in the email to reset your password.
  12. In the browser tab that opens, click Reset Password.
  13. In the Change Your Password screen, choose a new password and click the Change Password button.

Excellent! We've created a new Playground environment. Now, let's configure it as a Dev Hub so we can create scratch Orgs from it.

Setting up the Trailhead Environment as DevHub

  1. In the new Trailhead environment we created, click on the gear icon next to the bell icon and your profile picture, and select Setup.
  2. In the tab that opens, in the top left corner, in the Quick Find search field, enter the words "Dev Hub" and click on the result Dev Hub under Development.
  3. Move the Enable Dev Hub button to the Enabled state. Note! Once you activate this option, you cannot disable it!
  4. Move the Enable Unlocked Packages and Second-Generation Managed Packages button to the Enabled state. Note! Once you activate this option, you cannot disable it!
  5. Great! Now, let's create a new project in Visual Studio Code and connect our new DevHub environment to it.

Creating a New SFDX Project in Visual Studio Code

To create a new SFDX project in VS Code, follow these steps:

  1. Open Visual Studio Code.
  2. Press the keyboard shortcut Ctrl + Shift + P to open the command palette in VS Code.
  3. Type the word "Project" and select the option SFDX: Create Project.
  4. Choose the project template Standard.
  5. Give your project a name, for example, MyCoolProject.
  6. Select the directory where you want to save your new project.
  7. If VS Code displays a screen with the title Do you trust the authors of the files in this folder?, click the Yes, I trust the authors button.
  8. Great! Now, let's connect our DevHub environment to our project.

Connecting Our DevHub Environment to the Project as the Default DevHub Username

  1. If your terminal is not open in VS Code, press the shortcut Ctrl + ~ to open it.
  2. Type the following command in the Terminal to open and connect the Trailhead playground we created to our project as the Dev Hub.
    sfdx org:login:web -d -a MyDevHub
    Note the flag -d; this flag sets the org to which we are connecting, our Trailhead playground, as the default environment for creating scratch orgs in the project. Additionally, the -a flag allows us to define an alias for our environment. If we didn't use an alias, we would need to type the username of the environment in a cumbersome email format when referring to the environment in various commands.
  3. In the browser tab that opens, enter the username you saved earlier, and your password for the Trailhead playground. If the browser displays an "Allow Access?" message, click the "Allow" button. In the terminal, you will see a success message when you enter the environment.
  4. Great! We've connected our DevHub environment to the project. Now, all that's left to do is create a new scratch org from it.

Creating a New Scratch Org

To create a scratch org, follow these steps:

  1. In our project, under the config directory, there is a file named project-scratch-def.json. This file contains the settings for scratch orgs in our project. The code in the file should look something like this:
    {
                "orgName": "Demo company",
                "edition": "Developer",
                "features": ["EnableSetPasswordInApi"],
                "settings": {
                    "lightningExperienceSettings": {
                    "enableS1DesktopEnabled": true
                    },
                    "mobileSettings": {
                    "enableS1EncryptedStoragePref2": false
                    }
                }
    }
    To start fresh, change the value of "orgName" to something else, like "My Cool Company," and delete the "EnableSetPasswordInApi" value so that the "features" array remains empty.
    {
                "orgName": "My Cool Company",
                "edition": "Developer",
                "features": [],
                "settings": {
                    "lightningExperienceSettings": {
                    "enableS1DesktopEnabled": true
                    },
                    "mobileSettings": {
                    "enableS1EncryptedStoragePref2": false
                    }
                }
    }
  2. To create a new scratch org, type the following command in the Terminal:
    sfdx org create scratch -f config/project-scratch-def.json -d -a MyScratchOrg
    The org create scratch command creates a new scratch org in Salesforce. The flag -d specifies that this org is the current default org for the project, and all commands requiring an org will be directed to it. The -f flag points the command to the configuration file for scratch org in our project, and the -a flag specifies an alias for our scratch org.
  3. To open the scratch org we created in a browser, type the following command:
    sfdx org open
    Since we've set our scratch org as the default environment for the project, the Open command does not require us to specify the target environment.
  4. That's it! We've created a new scratch org. Whenever we want to create a new environment, whether in a new or existing project, all we need to do is ensure that our DevHub is configured in the project and then type a quick command to create a new scratch org!

Deleting a Scratch Org

Creating and deleting scratch orgs are routine actions in our development process in Salesforce. To delete a scratch org, we want to perform two steps:

  1. View the list of environments linked to our project. To do this, run the following command:
    sfdx org list
    The output of the command will display two tables: the Non-scratch orgs table and the Scratch orgs table. From the Scratch orgs table, we can copy the alias of the scratch org we want to delete.
  2. To delete a scratch org, run the following command in the Terminal along with the alias we copied from the scratch orgs table in the previous step:
    sfdx org delete scratch -o MyScratchOrg
    The -o flag specifies the alias or username of the org we want to delete. You can add the -p flag to this command to prevent the Terminal from showing a final confirmation message before deleting the scratch org. Use this option with caution to avoid accidentally deleting a scratch org that still has metadata that you haven't pulled to your local project.

That's it! In this post, we learned how to set up Trailhead as a DevHub, link it to our project, create a scratch org from it, and delete it when we're done using it.