Newer Version Available

This content describes an older version of this product. View Latest

Create Scratch Orgs

After you create the scratch org definition file, you can easily spin up a scratch org and open it directly from the command line.

Before you create a scratch org:

  • Set up your Salesforce DX project
  • Authorize the Dev Hub org
  • Create the scratch org definition file
You can create scratch orgs for different functions, such as for feature development, for development of packages that contain a namespace, or for user acceptance testing.

Delete any unneeded or malfunctioning scratch orgs in the Dev Hub org or via the command line so that they don’t count against your active scratch org allocations.

Tip

  1. Create the scratch org.
    To Run This Command
    Create a scratch org for development using a scratch org definition file The scratch org definition defines the org edition, features, org preferences, and some other options.
    1sfdx force:org:create -f project-scratch-def.json
    Specify scratch org definition values on the command line using key=value pairs
    1sfdx force:org:create adminEmail=me@email.com edition=Developer \
    2    username=admin_user@orgname.org
    Create a scratch org with an alias 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.
    1sfdx force:org:create -f project-scratch-def.json -a MyScratchOrg
    Create a scratch org for user acceptance testing or to test installations of packages In this case, you don’t want to create a scratch org with a namespace. You can use this command to override the namespace value in the scratch org definition file.
    1sfdx force:org:create -f project-scratch-def.json --nonamespace
    Indicate that this scratch org is the default CLI commands that are run from within the project use the default scratch org, and you don’t have to manually enter the username parameter each time.
    1sfdx force:org:create -f project-scratch-def.json --setdefaultusername
    Specify the scratch org’s duration, which indicates when the scratch org expires (in days). The default is 7 days. Valid values are from 1-30.
    1sfdx force:org:create -f config/project-scratch-def.json --durationdays 30
    1sfdx force:org:create -f config/project-scratch-def.json -d 3

    Indicate the path to the scratch definition file relative to your current directory. For sample repos, this file is located in the config directory.

    Stdout displays two important pieces of information: the org ID and the username.

    1Successfully created scratch org: 00D3D0000000PE5UAM, 
    2    username: test-b4agup43oxmu@example.com

    If the create command times out before the scratch org is created (the default wait time is 6 minutes), you see an error. Issue this command to see if it returns the scratch org ID, which confirms the existence of the scratch org:

    1sfdx force:data:soql:query -q "SELECT ID, Name, Status FROM ScratchOrgInfo \
    2    WHERE CreatedBy.Name = '<your name>' \
    3    AND CreatedDate = TODAY" -u <Dev Hub org>

    This example assumes that your name is Jane Doe, and you created an alias for your Dev Hub org called DevHub:

    1sfdx force:data:soql:query -q "SELECT ID, Name, Status FROM ScratchOrgInfo \
    2    WHERE CreatedBy.Name = 'Jane Doe' AND CreatedDate = TODAY" -u DevHub

    If that doesn’t work, create another scratch org and increase the timeout value using the --wait parameter. Don’t forget to delete the malfunctioning scratch org.

  2. Open the org.
    1sfdx force:org:open -u <username/alias>
    If you want to open the scratch org in Lightning Experience or open a Visualforce page, use the --path parameter.
    1sfdx force:org:open --path lightning
  3. Push local project source to your scratch org.