Sub topic 4
Scratch Orgs and the Dev Hub
This chapter assumes you know the difference between production and non-production orgs, can deploy metadata, and can use a command line and Git. It covers what a scratch org is, how the Dev Hub fits in, and how to create and discard one with the Salesforce CLI.
What a scratch org is
A scratch org is a temporary Salesforce org that you create from a definition file. It starts empty of your production data and configuration. You build its state from source files kept in version control. Because it is disposable, you can create one for a single feature or test run and delete it afterward.
This differs from a sandbox. A sandbox is copied from a source org, so it carries that org's metadata and, depending on type, some data. A scratch org is built from your project source and a definition file.
The Dev Hub
A Dev Hub is an org that creates and tracks scratch orgs. You usually enable it in your production org, or in a dedicated org, through Setup. Scratch orgs belong to the Dev Hub that created them, and the Dev Hub enforces the allocation limits for them.
You must authorize the Dev Hub in the CLI before you can create scratch orgs. The --set-default-dev-hub flag means later commands use it automatically.
Creating a scratch org
The definition file sets the org's shape. This minimal example names the org, picks an edition, skips sample data, and enables one setting.
{
"orgName": "Acme Development",
"edition": "Developer",
"hasSampleData": false,
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Save it as config/project-scratch-def.json. Then run this sequence from your project directory.
sf org login web --set-default-dev-hub --alias devhub
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-default
sf project deploy start
sf org open
sf org delete scratch --target-org my-scratch --no-promptEach command does one job:
sf org login webopens a browser so you can log in to the Dev Hub. The CLI stores the authorization under the aliasdevhub.sf org create scratchbuilds the org from the definition file. It lasts seven days and becomes the default org for later commands.sf project deploy startdeploys your project source to the default org.sf org openopens the scratch org in a browser.sf org delete scratchremoves the org. The--no-promptflag skips the confirmation question.
Common errors
- No Dev Hub found. The CLI cannot find an authorized Dev Hub. Run the login command again with
--set-default-dev-hub, or pass--target-dev-hub. - Dev Hub not enabled. The org you logged in to has not had Dev Hub turned on in Setup.
- Invalid setting in the definition file. A setting name or value is wrong for the chosen edition or API version. Fix the file and create a new org.
- Allocation limit reached. The Dev Hub allows only a set number of active scratch orgs and daily creations. Delete orgs you no longer need.
- Deploying to the wrong org.
sf project deploy startuses the default org. Check which org is default before you deploy.
Why this model helps
A scratch org gives each developer or build a clean, repeatable environment. Everyone starts from the same definition file and source, so differences between environments come from the code and not from leftover manual changes. Later chapters cover lifespan and limits, source tracking, and when to choose a sandbox instead.
Exercise
A teammate creates a scratch org, configures a new validation rule directly in the org's Setup, and then deletes the org the next day. What happened to the validation rule, and what should they have done?
Show answer
The rule was lost, because a scratch org holds only what was deployed to it or configured in it, and deleting the org removes all of it. Before deleting, they should have retrieved the rule's metadata into the project source and committed it to version control.