Sub topic 5
Shaping a scratch org with a definition file
This chapter assumes you can run sf commands, have authorized a Dev Hub (see the previous chapter), and know what a Salesforce project directory looks like. You should also be comfortable reading JSON. By the end you will be able to write a definition file, create an org from it, and diagnose the common reasons a creation fails.
What the definition file does
A scratch org is built from scratch each time you create one. It has no data and no customizations from any other org. The scratch org definition file is the recipe that tells Salesforce what kind of org to build. Salesforce reads it once, at creation time.
By convention the file lives at config/project-scratch-def.json. You can use any path, because you pass it to the CLI explicitly. Keep the file in version control with the rest of the project. Then every developer and every CI job starts from the same org shape, which is what makes scratch orgs reproducible.
The file does not hold your application's metadata. Custom objects, Apex classes, and flows belong in your source directory and are deployed after the org exists. The definition file covers only the org itself: its edition, its licensed features, and its org-level settings.
A minimal definition file
This is the smallest file that is useful. It sets the org name, the edition, whether sample data is loaded, and one setting.
{
"orgName": "Acme Development",
"edition": "Developer",
"hasSampleData": false,
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Each key does one job:
orgNameis the company name shown in the org. It has no effect on behavior.editionsets the base feature set and limits. Developer is the usual choice. Other values include Enterprise, Group, and Professional, and the Partner variants of each.hasSampleDatacontrols whether the org includes the sample accounts, contacts, and similar records that Salesforce provides. Setting it tofalsegives you an org with no records.settingsholds org-level configuration, described below.
Optional top-level keys
Beyond the minimal file, these keys come up most often.
| Key | Purpose | Example |
|---|---|---|
| `features` | Enables licensed or optional features that the edition does not turn on by default | `["MultiCurrency", "Communities"]` |
| `country` | Sets the default country for the org | `"US"` |
| `language` | Sets the default language | `"en_US"` |
| `username` | Sets a fixed username instead of a generated one | `"[email protected]"` |
| `adminEmail` | Sets the email address for the admin user | `"[email protected]"` |
| `description` | Free-text note about the org's purpose | `"Feature work for billing"` |
| `release` | Chooses the Salesforce release: `Preview` or `Previous` | `"Preview"` |
Two notes on this table. First, a fixed username must be unique across all of Salesforce. Creation fails if someone already has it, so most teams leave it out and let Salesforce generate one. Second, release is how you test against an upcoming release during the preview window. Leave it out for normal work, and the org uses the current release.
Features versus settings
New users often confuse features and settings, and the difference matters when creation fails.
- A feature is something the org is licensed for or has provisioned. Multiple currencies, Experience Cloud sites (
Communities), and similar capabilities are features. You list them as strings in an array. - A setting is a configuration choice within what the org already has. Settings are nested objects whose names and fields follow the Metadata API settings types.
Here is a fuller file that uses both.
{
"orgName": "Acme Billing Dev",
"edition": "Developer",
"description": "Scratch org for billing feature work",
"country": "US",
"language": "en_US",
"hasSampleData": false,
"features": ["MultiCurrency", "EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
},
"securitySettings": {
"passwordPolicies": {
"enableSetPasswordInApi": true
}
}
}
}The pairing of EnableSetPasswordInApi as a feature and enableSetPasswordInApi as a setting is a real case where the two work together. The feature makes the option available, and the setting turns it on. Not every setting needs a matching feature, but some do. When a setting you have written is rejected, check whether it depends on a feature you did not list.
Finding the right setting name
Setting names are not invented by the CLI. Each top-level key under settings is a Metadata API settings type, such as securitySettings or lightningExperienceSettings. The fields under it match the fields of that type. To find the right name:
- Look up the settings type in the Metadata API reference.
- Find the field that corresponds to the checkbox you would click in Setup.
- Use the field name exactly as written, with the same capitalization.
A faster route is to configure a scratch org by hand in Setup, retrieve its settings metadata with sf project retrieve start, and copy the relevant fields into your definition file.
Creating the org
Pass the file to sf org create scratch. The flags below also name the org with an alias and set its lifespan.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-defaultWhat each flag does:
--definition-filepoints at your JSON file.--aliasgives the org a short name, so later commands can say--target-org my-scratch.--duration-dayssets how long the org lives, from 1 to 30 days. The default is 7.--set-defaultmakes this the default target org for later commands in this project directory.
The command contacts your default Dev Hub, so you must have authorized one. If you have several Dev Hubs, add --target-dev-hub to name the one you want.
You can skip the file for quick experiments by using --edition developer instead of --definition-file. This gives you a plain org with no features or settings. It is fine for a throwaway test, but you lose reproducibility, so avoid it for anything a teammate will repeat.
After creation, deploy your source and open the org.
sf project deploy start
sf org openUse sf org list at any point to see your orgs, their aliases, and their expiration dates.
Validating before you commit the file
A definition file that works today can break after a Salesforce release, because feature names and setting fields change. A few habits keep the file healthy.
- Create an org from the file whenever you edit it, not only when a new developer joins. A bad edit then fails on your machine instead of in CI.
- Keep the file small. Add a setting only when your source depends on it. A short file has fewer ways to break.
- Review changes to the file in pull requests, as you would any code change. A one-line edit can alter every future org.
- Remember that JSON does not allow comments or trailing commas. Put the explanation in
descriptionor in a README.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Creation fails with an unknown feature or setting error | The name is misspelled, the wrong case, or no longer supported in the release | Check the name against the Metadata API reference and the current release notes |
| Creation fails with a JSON parse error | A comment, a trailing comma, or a missing quote | Run the file through a JSON validator |
| A setting is silently not applied | The setting depends on a feature you did not list | Add the required feature and create a new org |
| Deployment fails right after creation | Your source uses a feature the org does not have | Add the feature to the file so the org matches what the source needs |
| Creation fails because the username is taken | A fixed `username` collides with another org | Remove the `username` key |
| Creation is refused for allocation reasons | You have hit the active scratch org limit | Delete unused orgs, as the limits chapter describes |
The fourth row is the most common in practice. The definition file and the source must agree. If your Apex references a standard object that only exists with a certain feature, the deploy fails until the feature is in the file. Treat that failure as a signal that the file is incomplete.
Keeping the file aligned with production
Scratch orgs only help you if they resemble where your code will run. Edition, features, and key settings should match your production org closely enough that code behaves the same way. When production gains a feature your code relies on, update the definition file in the same pull request that adds the code. That way the file does not drift away from reality, and a developer cannot unknowingly build against an org that lacks something production has.
The definition file cannot copy everything. It does not bring over production's data, its users, or its full configuration. When you need that level of fidelity, a sandbox is the better fit, as covered in the decision chapter.
Exercise
You add "features": ["MultiCurrency"] to your definition file, then run sf org create scratch again with the same alias settings. A teammate says your existing scratch org will now have multiple currencies enabled after you run sf project deploy start. Is the teammate correct? Explain what you should do.
Show answer
No. The definition file is read only when an org is created, and deploying source does not reapply it. The existing org stays as it was. To get multiple currencies, create a new scratch org from the updated file (deleting the old one if you are near your allocation limit), then deploy your source to the new org.
Exercise
A scratch org is created successfully, but the deploy fails because an Apex class references a field that only exists when a certain feature is enabled. Which file do you change, and why is this better than enabling the feature by hand in Setup?
Show answer
Change the scratch org definition file by adding the needed feature (and any related setting), then create a new org. Enabling the feature by hand fixes only one org and is not recorded anywhere. Putting it in the definition file, which is under version control, means every future org and every CI run gets the same configuration.