Sub topic 8
Writing a scratch org definition file
A scratch org definition file is a JSON file that tells the Dev Hub what kind of scratch org to build. This chapter assumes you have a Salesforce DX project, you can read JSON, and you have authorized a Dev Hub with the CLI.
Where the file lives
sf project generate creates config/project-scratch-def.json for you. The file name is only a convention. You pass its path to sf org create scratch --definition-file, so you can keep several files, such as one for a Developer edition org and one for an Enterprise edition org.
A working example
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Each top-level key controls one part of the new org:
| Key | Type | Purpose |
|---|---|---|
| `orgName` | string | The company name shown in the scratch org. |
| `edition` | string | The org edition, such as `Developer`, `Enterprise`, `Group`, or `Professional`. |
| `features` | array of strings | Optional features to turn on. |
| `settings` | object | Org settings, grouped by settings name. |
edition is the only key you must set to get a usable org. Everything else adjusts the defaults.
Features and settings
Features and settings do different jobs.
- A feature is a capability that the edition does not include by default. You list it by name in the
featuresarray. Feature names are case-insensitive, but a misspelled or unsupported name makes creation fail. - A setting is a configuration choice inside the org. Settings are nested objects. The outer key is the settings group, such as
lightningExperienceSettings. The inner keys are the individual options for that group.
In the example, EnableSetPasswordInApi lets you set a user's password through the API. The enableS1DesktopEnabled setting turns on Lightning Experience for desktop users.
How the pieces fit together
Common errors
- Invalid JSON. A trailing comma or a missing quote stops the command before it contacts the Dev Hub. Open the file in an editor with JSON validation.
- Unknown feature. Features that need a license your Dev Hub lacks are rejected. Remove the feature or use a Dev Hub that has the license.
- Wrong settings name. A typo in a settings group or option name causes a deployment error while the org is being built.
- Wrong path. The path is relative to the directory where you run the command. Run it from the project root so
config/project-scratch-def.jsonresolves.
Start small. Add one feature or setting at a time and create a test org after each change. A failure then points to the last thing you added.
Exercise
You add the feature name "EnableSetPasswordInApi" to the "settings" object instead of the "features" array. What goes wrong, and where should it go?
Show answer
Feature names are not settings groups, so the Dev Hub does not recognize the entry and org creation fails or ignores it. Put the name in the "features" array at the top level of the file, for example "features": ["EnableSetPasswordInApi"].
Exercise
Which key is the only one you must set to get a usable scratch org, and what does it control?
Show answer
The "edition" key. It sets the org edition, such as Developer or Enterprise. The other keys adjust the defaults.