Sub topic 2
Sandboxes versus scratch orgs
This chapter assumes you know what a sandbox is and have used the Salesforce CLI at a basic level. It compares sandboxes with scratch orgs, the other kind of non-production org. The comparison helps you decide when each one fits.
Two ways to get a non-production org
A sandbox is a copy of your production org. It always starts from production's metadata, and depending on the type it can also include data. It lasts until you delete it, and you can refresh it to reset it to production's current state.
A scratch org is a temporary org built from a definition file. It contains no production metadata or data. You create it from a Dev Hub, which is a feature enabled in an org (usually production) that tracks and authorizes scratch orgs. It expires after 1 to 30 days, and the default is 7.
A scratch definition file looks like this:
{
"orgName": "Feature Work",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Create the org with the CLI:
sf org create scratch --definition-file config/project-scratch-def.json --alias feature1 --target-org devhubKey differences
| Aspect | Sandbox | Scratch org |
|---|---|---|
| Starting point | Copy of production | Definition file, empty org |
| Production data | Depends on type | Never |
| Lifetime | Until deleted | 1 to 30 days |
| Reset method | Refresh | Delete and create a new one |
| Created from | Production or another sandbox | Dev Hub |
| Typical use | Testing, QA, training, staging | Source-driven development, CI |
How to choose
Use a sandbox when the work depends on what production actually looks like. Examples are testing against real configuration, running UAT with realistic data, checking integrations, and performance testing at production volume. Only sandboxes can carry production data.
Use a scratch org when you want a clean, reproducible environment. Your source code in version control defines the org, so each developer or CI run gets an identical one. Scratch orgs suit feature branches and automated test runs.
Many teams use both. Developers build in scratch orgs, then changes are deployed through sandboxes such as QA and staging before they reach production.
Common mistakes
- Expecting production data in a scratch org. Scratch orgs have none. You must load test data yourself.
- Treating a scratch org as long-lived. It expires. Keep all work in source control, not only in the org.
- Missing metadata in scratch definitions. Your production org may have settings that the definition file doesn't enable. The code then behaves differently than it would in a sandbox.
- Using a sandbox when a clean org would be enough. Sandbox creation and refresh can take much longer, and refresh intervals limit how often you can reset one.
Exercise
A team needs to run user acceptance testing against realistic customer records and production configuration. Should they use a sandbox or a scratch org, and why?
Show answer
A sandbox. It starts as a copy of production metadata, and a sandbox type that copies data can supply realistic records. A scratch org is built from a definition file and never contains production data.
Exercise
A developer's CI pipeline needs a fresh, identical org for each test run. Which org type fits, and how is it reset between runs?
Show answer
A scratch org. It is created from a definition file, so every run gets the same starting state. Instead of refreshing it, you delete it and create a new one.