Sub topic 8
The scratch org definition file
This chapter assumes you have generated a Salesforce DX project, can read sfdx-project.json, and have the Salesforce CLI (sf) installed with a Dev Hub org authorized. A Dev Hub is the org that creates and tracks scratch orgs.
What the file does
A scratch org is a temporary, source-driven Salesforce org. The scratch org definition file tells the CLI what shape that org should have: its edition, its enabled features, and its settings. The generated project puts this file at config/project-scratch-def.json.
The file is separate from sfdx-project.json. The project file describes your source. The definition file describes the org that receives the source. The two meet when you create an org and then deploy.
A minimal definition file
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": [],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Each top-level key has a job:
| Key | Purpose |
|---|---|
| `orgName` | The company name shown in the new org. |
| `edition` | The org edition, such as `Developer`, `Enterprise`, `Group`, or `Professional`. |
| `features` | A list of feature names to enable, such as `EnableSetPasswordInApi`. |
| `settings` | Org settings, grouped by settings type, that mirror the Metadata API settings. |
You can also set description, country, language, hasSampleData, and release. Keep the file to what your project needs. Every extra feature is one more thing that can fail at creation time.
Features and settings
Features turn on capabilities that a bare edition lacks. They are plain strings in the features array. Some take a value, for example "Communities" or "Walkthroughs".
Settings change org behavior. Each key under settings names a settings type, and its children use the same field names as the Metadata API. This is why the sample enables Lightning Experience under lightningExperienceSettings. If a setting needs a feature, add the feature as well, or creation fails.
Connecting the pieces
The CLI does not search for the definition file. You pass its path when you create the org. The project root matters only because you run the command from there and the path is relative.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default
sf project deploy start
sf project retrieve startThe flow works in three steps:
sf org create scratchreads the definition file and asks the Dev Hub for an org with that edition, features, and settings.--aliasgives the org a short name, and--set-defaultmakes it the target for later commands.sf project deploy startpushes source from the package directories insfdx-project.jsoninto that org.sf project retrieve startpulls changes back.
Common errors
- Invalid JSON. A trailing comma or missing quote stops creation before the Dev Hub is contacted. Validate the file in your editor.
- Unknown feature or setting name. The error names the entry. Check spelling and case against the Salesforce documentation for scratch org features and settings.
- Setting without its feature. The org is rejected or the setting is ignored. Add the required feature.
- No Dev Hub. Creation fails if no Dev Hub is authorized. Run
sf org listto check. - Wrong path. If you run the command outside the project root, the relative path does not resolve.
Exercise
You add "enableS1DesktopEnabled": true directly under settings in the definition file, and org creation fails. What is wrong, and how do you fix it?
Show answer
Settings must be grouped under their settings type. Nest the field inside lightningExperienceSettings, so the file has "settings": { "lightningExperienceSettings": { "enableS1DesktopEnabled": true } }.
Exercise
Which command reads the definition file, and which command sends your package directory source to the new org?
Show answer
sf org create scratch --definition-file config/project-scratch-def.json reads the definition file. sf project deploy start sends the source from the package directories in sfdx-project.json.