Sub topic 8
Writing a scratch org definition file
This chapter assumes you have a Dev Hub authorized in the Salesforce CLI (sf) and a Salesforce DX project. You should also be comfortable reading and writing JSON. By the end you will be able to write a definition file that produces the scratch org you want, and to find the cause when it doesn't.
What the definition file does
A scratch org is created empty, and the definition file describes how to build it. When you run sf org create scratch, the CLI sends the file's contents to the Dev Hub. The Dev Hub provisions an org of the requested edition, turns on the listed features, and applies the listed settings.
sf project generate creates a starter file at config/project-scratch-def.json. You can keep it, edit it, or add more files under any names you like. Teams often keep several, such as config/dev-scratch-def.json and config/ci-scratch-def.json. You pick one with --definition-file.
Anatomy of a minimal file
The file used in this tutorial looks like this:
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Each top-level key has a job:
orgNameis the company name shown in the org. It is a label only.editionsets the base product, for exampleDeveloper,Enterprise,Group, orProfessional.featuresis an array of strings. Each string turns on one capability.settingsis an object that mirrors Metadata API settings types.
The file must be strict JSON. Comments and trailing commas are not allowed, and a single stray comma makes the whole file unreadable.
Common top-level properties
Many other properties are optional. These are the ones you will use most.
| Property | Type | Purpose |
|---|---|---|
| `edition` | string | Base edition of the org |
| `orgName` | string | Company name in the org |
| `description` | string | Free-text note about the org's purpose |
| `country` | string | Two-letter ISO country code, such as `US` |
| `language` | string | Default language code, such as `en_US` |
| `hasSampleData` | boolean | Whether to include sample records. The default is `false`. |
| `release` | string | `Preview` or `Previous` to target a different release than the Dev Hub's |
| `username` | string | A fixed admin username instead of a generated one |
| `adminEmail` | string | Email address for the admin user |
| `features` | array | Capabilities to enable |
| `settings` | object | Org settings to apply |
Be careful with username. Usernames must be unique across all of Salesforce. A fixed value works for one org, but a second org with the same name fails. For that reason most teams leave username out and let Salesforce generate one.
The release property exists for testing against an upcoming release. During preview windows, a Dev Hub on the current release can create orgs on the preview release. Leave it out unless you need that.
Features: turning capabilities on
A feature is a named capability that a plain edition does not include by default. Examples are MultiCurrency, Communities, and EnableSetPasswordInApi.
Some points to keep in mind:
- Feature names are strings, not booleans. You list the ones you want, and you don't set the others to false.
- Some features take a value, written as
Name:value. For example,"Walkthroughs:Enabled"is not needed by most projects, but the pattern shows up in the documentation, so don't be surprised by a colon. - A feature your edition doesn't support causes creation to fail, with an error naming the feature.
- Some features only change behavior when a matching setting is also set. Enabling the feature makes the capability available, and the setting configures it.
When you add a feature to a file, create an org from it right away. One new feature per attempt makes a failure easy to trace.
Settings: configuring behavior
The settings object has one key for each settings type. Inside each key are the individual properties of that type. The names come from the Metadata API, so they use camelCase, and the nesting follows the metadata structure.
In the example above, lightningExperienceSettings is the settings type and enableS1DesktopEnabled is a property on it. Together they enable the Lightning Experience UI.
Here is a larger example that sets a default language and country, adds translation support, and includes sample data:
{
"orgName": "Translation Sandbox",
"edition": "Enterprise",
"country": "US",
"language": "en_US",
"hasSampleData": true,
"description": "Org for testing translated labels",
"features": ["MultiCurrency"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
},
"languageSettings": {
"enableTranslationWorkbench": true
}
}
}Settings type names and property names must match the Metadata API exactly, including capitalization. A misspelled key is not silently ignored. Creation fails with an error that names the unknown or invalid setting.
Finding the right setting name
You often know what you want from the Setup UI but not the property name. Two approaches work well:
- Look up the settings type in the Metadata API Developer Guide. Each settings type lists its fields, and each field name is what you use in the file.
- Configure an org by hand, retrieve its settings metadata with
sf project retrieve start, and copy the field names from the retrieved XML.
Retrieved XML uses element names like <enableTranslationWorkbench>true</enableTranslationWorkbench>. The element name becomes the JSON key, and the value becomes the JSON value.
Validate before you create
Creating a scratch org takes a few minutes, so catch syntax errors locally first. Any JSON parser works. This command uses Python's built-in one and prints the file if it is valid:
python3 -m json.tool config/project-scratch-def.jsonIf the file is invalid, the parser reports the line and column of the first error. This catches missing commas, unquoted keys, and unbalanced braces. It cannot catch a wrong setting name or an unsupported feature. Only the Dev Hub knows those, and you find out when you create the org.
Then create the org with your file:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-defaultCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The CLI reports invalid JSON | Trailing comma, comment, or single quotes | Run the file through a JSON parser and fix the reported line |
| Error naming an unknown feature | Misspelled feature name or one not available in the edition | Check the spelling, or change `edition` |
| Error naming an invalid setting | Wrong settings type or property name, or a wrong value type | Compare against the Metadata API reference |
| Username already in use | A fixed `username` was reused | Remove `username` or make it unique |
| Org created, but a behavior is missing | Feature enabled without its matching setting | Add the setting, then recreate the org |
The last row matters because a definition file is applied only at creation time. If you change the file, existing scratch orgs do not change. You must create a new org to see the effect, and you should delete the old one to free its slot.
Keeping files maintainable
A few habits keep definition files useful over time:
- Commit the file to version control with the rest of the project, so every developer and CI job builds the same org.
- Keep the file small. Enable only what the project needs, because each added feature or setting is another way for creation to fail.
- Use a
descriptionto record why unusual features or settings are present. - Keep separate files for different purposes, such as a lean one for CI and a fuller one for exploratory work.
Exercise
You add "settings": { "languageSettings": { "enableTranslationWorkBench": true } } to your definition file, and scratch org creation fails with an error about an invalid setting. What is the most likely cause, and how do you confirm and fix it?
Show answer
The property name has the wrong capitalization. The Metadata API field is enableTranslationWorkbench, with a lowercase "b" in "Workbench". Names must match exactly. Confirm by comparing the key against the LanguageSettings reference in the Metadata API Developer Guide, correct the key, validate the JSON locally, and run the create command again.
Exercise
You edit the definition file to add the MultiCurrency feature while a scratch org from the old file is still your default org. Will that org gain multi-currency support? What should you do?
Show answer
No. The definition file is read only when the org is created, so the existing org does not change. Create a new scratch org from the updated file, switch to it, and delete the old org with sf org delete scratch to free its active slot.