Scratch Org is temporary deployment of Salesforce source code and metadata. Any number of Salesforce Scratch Org’s can be created to start new project, to start a new feature branch, to test a new feature, to start automated testing, to perform development tasks directly in an Org and to start form “scratch” with a fresh new Org.

In this tutorial, we will create Salesforce Scratch Org using command line in Terminal (Mac). Follow the steps given below:

  • As already we have created Salesforce DX project from existing source (GitHub), now open Terminal and enter the following code

Create Salesforce Scratch Org

In this step, we are going to create Salesforce Scratch Org in Command Line Interface(CL) which will be used in our future development. Before creating a Scratch Org, check that developer hub account is linked to CLI.

Enter the following code and the result will be as shown below.

Steps to create Salesforce Scratch Org

  1. Create directory.
  2. Clone the repository (dream house) from GitHub.
  3. Set a default username for the Org that you connect.
  4. Create a Scratch Org.
  5. Push source metadata to Salesforce Scratch Org.
  6. Assigning permissions to DreamHouse App.
  7. Importing test data.
  8. Open Scratch Org.

To create a scratch Org from CLI, enter cd into the project before running the following code.

Setting default username to the Org

Salesforce recommends developers to set default username to the Org that we connect during development. CLI requires username to create a scratch Org, to synchronise source code between project and Scratch Org and other task to determine which Org that we are connecting.

Now when setting default username to the Org, we are not required to specify a username, the command used the default.

Output

Creating Salesforce Scratch Org with alias name

Scratch org usernames are long and unintuitive. Setting an alias each time you create a scratch org is a great way to track the scratch org’s function. And it’s much easier to remember when issuing subsequent CLI commands.

Run the following command to create Salesforce Scratch Org with alias

Output

Push Source Metadata to Scratch Org

After changing the source, we can sync the changes to our Scratch Org by pushing the changed source to it. All sources in the folders in the package directories are pushed to the Scratch Org. Run the following command to push source metadata to Scratch Org.

Assigning Permset to Dreamhouse App

Before accessing DreamHouse app in Salesforce, we need to assign the Permset using CLI. Run the following command shown below.

Import Test Data

We are going to import test data using SObject Tree API. Run the following command in CLI.

We have successfully created and configured Salesforce scratch Org. In our final step, open the Scratch Org.

Open Salesforce Scratch Org

To open Salesforce Scratch Org, run the following command : sfdx force:org:open. To open the DreamHouse app, click App launcher and then click DreamHouse icon as shown below.

Create Salesforce Scratch Org - Opening Dreamhouse App