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.

prasanth:sfdx-dreamhouse prasanth$ sfdx force:org:list
=== Orgs
     ALIAS   USERNAME         ORG ID              CONNECTED STATUS
???  ??????  ???????????????  ??????????????????  ????????????????
(D)  DevHub  00D6F000002SEW1UAO  Connected

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.

prasanth:~ prasanth$ cd tutorialkart_sfdxproject

prasanth:tutorialkart_sfdxproject prasanth$ cd sfdx-dreamhouse

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.

prasanth:sfdx-dreamhouse prasanth$ sfdx force:config:set


=== Set Config
NAME             VALUE
???????????????  ???????????????

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

prasanth:sfdx-dreamhouse prasanth$ sfdx force:org:create
 -s -f config/project-scratch-def.json -a testScratchOrg


Successfully created scratch org: 00DO00000055j0XMAQ, 

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.

prasanth:sfdx-dreamhouse prasanth$ sfdx force:source:push
=== Pushed Source

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.

prasanth:sfdx-dreamhouse prasanth$ sfdx force:user:permset:assign -n Dreamhouse
=== Permsets Assigned
?????????????????????????????  ?????????????????????????  Dreamhouse

Import Test Data

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

prasanth:sfdx-dreamhouse prasanth$ sfdx force:data:tree:import --plan data/sample-data-plan.json

[Image:]=== Import Results
REFERENCE ID           TYPE         ID
?????????????????????  ???????????  ??????????????????
CarolineBrookerRef     Broker__c    a01p0000006rpGHAAY
MichaelJonesRef        Broker__c    a01p0000006rpGIAAY
JonathanBradleyRef     Broker__c    a01p0000006rpGJAAY
JenniferWuRef          Broker__c    a01p0000006rpGKAAY
OliviaGreenRef         Broker__c    a01p0000006rpGLAAY
MiriamAupontRef        Broker__c    a01p0000006rpGMAAY
MichelleLambertRef     Broker__c    a01p0000006rpGNAAY
SeniorBrokerRef        Broker__c    a01p0000006rpGOAAY
18HenryStRef           Property__c  a04p0000009UwhxAAC
24PearlStRef           Property__c  a04p0000009UwhyAAC
72FrancisStRef         Property__c  a04p0000009UwhzAAC
32PrinceStRef          Property__c  a04p0000009Uwi0AAC
110BaxterStRef         Property__c  a04p0000009Uwi1AAC
448HanoverStRef        Property__c  a04p0000009Uwi2AAC
127EndicottStRef       Property__c  a04p0000009Uwi3AAC
48BrattleStRef         Property__c  a04p0000009Uwi4AAC
121HarborwalkRef       Property__c  a04p0000009Uwi5AAC
640HarrisonAveRef      Property__c  a04p0000009Uwi6AAC
95GloucesterStRef      Property__c  a04p0000009Uwi7AAC
145CommonwealthAveRef  Property__c  a04p0000009Uwi8AAC

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