Sub topic 5
Shaping a Scratch Org with a Definition File
This chapter assumes you can run basic Salesforce CLI commands, understand metadata and deployment, and have a Dev Hub authorized. The earlier chapters on the Dev Hub covered why scratch orgs exist. Here you control what a new scratch org looks like when it is created.
What the definition file does
A scratch org starts empty. Its edition, features, and settings come from a JSON file that you pass to the create command. By convention the file is config/project-scratch-def.json, and you keep it in version control with your source. Because the file is the recipe, any teammate or CI job gets the same starting point.
Here is a minimal file.
{
"orgName": "Acme Development",
"edition": "Developer",
"hasSampleData": false,
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}The main properties
| Property | Purpose |
|---|---|
| `orgName` | The company name shown in the org. |
| `edition` | The base edition, such as Developer, Enterprise, Group, or Professional. |
| `hasSampleData` | Whether the org includes sample records such as accounts and contacts. |
| `features` | A list of optional features to turn on, for example `EnableSetPasswordInApi`. |
| `settings` | Org settings, written in the same shape as the metadata API settings. |
| `country`, `language` | Locale values for the org. |
Two properties have the most effect on behavior. Setting edition decides which standard objects and limits you get, so choose the edition your production org uses. Setting hasSampleData to false gives you a clean org, which is better for automated tests because they will not depend on records you did not create.
Features and settings
features and settings do different jobs. A feature enables a capability that is not on by default, such as multi-currency. A setting configures behavior inside the org, such as whether Lightning Experience is the default interface. Settings use the structure of the corresponding Settings metadata type. For example, lightningExperienceSettings maps to the LightningExperienceSettings type.
The order of operations matters. Features and settings are applied when the org is created, before you deploy any source. If your code depends on a feature, such as a field type that needs multi-currency, the feature must be in the definition file. Otherwise the first deployment fails.
Creating the org
This sequence authorizes a Dev Hub, creates a seven-day scratch org from the file, deploys the project source, opens the org, and deletes it.
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-promptThe --alias flag gives the org a short name for later commands. The --set-default flag makes it the default target, so sf project deploy start and sf org open need no --target-org flag. The --duration-days flag sets the lifespan, which later chapters cover in detail.
Common errors
- Invalid setting name. A misspelled or unsupported key in
settingsmakes creation fail. Compare the key with the Settings metadata type in the Metadata API reference. - Missing feature. Deployment fails with an error about an unknown field or object. Add the required feature to the
featureslist and create a new org. You cannot add most features to an org that already exists. - Edition mismatch. Code works in your scratch org but fails in production because the editions differ. Match the
editionto production. - Editing the file and expecting changes. The definition file is read only at creation time. Changing it does not alter orgs that already exist.
Summary
The definition file is the repeatable starting state of a scratch org. It sets the edition, sample data, features, and settings, and is applied once at creation. Put it in Git and treat changes to it like any other code change.
Exercise
A teammate adds a multi-currency field to the project source, and deployment to your existing scratch org fails. You then add the multi-currency feature to the definition file, but the same deployment still fails in that org. Why, and what should you do?
Show answer
The definition file is read only when the org is created, so editing it does not change an existing org. Delete the scratch org, create a new one from the updated definition file, and deploy the source again.