Sub topic 2
Authorizing the Dev Hub and Creating a Scratch Org
Assume that sf is installed and that Dev Hub is enabled in a supported Salesforce org. You also need permission to authorize that org and create scratch orgs.
Authorizing the Dev Hub from the CLI
A Dev Hub org is the central org that creates and tracks scratch orgs. A scratch org is a temporary Salesforce org for development and testing. Authorize the Dev Hub first, then save a local alias for it.
sf --version
sf org login web --set-default-dev-hub --alias my-hubThe second command opens a browser login. After you sign in, my-hub identifies the Dev Hub in later commands. The --set-default-dev-hub option makes it the default Dev Hub.
flowchart TD
A[Dev Hub enabled in Setup] --> B[Authorize with sf]
B --> C[Generate DX project]
C --> D[Write scratch definition]
D --> E[Create scratch org]Generating a Salesforce DX Project
A Salesforce DX project is a directory with the metadata and configuration needed for Salesforce development. Generate one before creating the scratch org definition.
sf project generate --name my-project
cd my-projectThe generated project includes config/project-scratch-def.json. This file describes the scratch org that Salesforce should create.
Writing a Scratch Org Definition File
Edit config/project-scratch-def.json so it contains the required org name, edition, features, and settings:
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": [
"EnableSetPasswordInApi"
],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}The edition selects the Salesforce edition. A feature enables an optional capability. A setting changes an org-level behavior. Available features and settings depend on Salesforce support for scratch orgs.
Creating Your First Scratch Org
Run this command from the project directory:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias my-scratch \
--duration-days 7 \
--set-defaultSalesforce creates the org from the definition file. The alias my-scratch provides a readable name, and --set-default makes it the default target for later project commands. Scratch orgs are temporary. This one expires after seven days unless you delete it earlier.
Exercise
Which command creates a seven-day scratch org and makes it the default target?
Show answer
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-default