Sub topic 2
Scratch orgs and sandboxes: what starts empty and what is copied
This chapter assumes you know the Salesforce data model, have the sf CLI authorized to a Dev Hub, and recognize a Salesforce DX project layout. The two environments get their data in different ways, so you pick a seeding approach based on which one you are loading.
Scratch orgs start empty
A scratch org is a disposable org that you create from a definition file. It holds the configuration that the definition file and your deployed metadata describe. It holds no records from production.
Two things follow from this:
- Every scratch org needs seeding. A new org has no Accounts, no Contacts, and no custom object rows.
- Metadata must be deployed before data. Objects and fields have to exist before a record can be inserted into them.
The usual order is: create the org, deploy the project, then load data.
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias my-scratch \
--set-default \
--duration-days 7
sf project deploy start --target-org my-scratchScratch orgs expire. With --duration-days 7 the org and its data are deleted after seven days, so your seed process must be quick to repeat.
Sandboxes are copies of production
A sandbox is a copy of a production org. What it contains depends on its type.
| Sandbox type | Metadata copied | Data copied |
|---|---|---|
| Developer | Yes | None |
| Developer Pro | Yes | None |
| Partial Copy | Yes | A sample of records, selected by a sandbox template |
| Full | Yes | All records, with optional field history |
A Developer sandbox looks like a scratch org in one respect: it has your configuration and no business records. A Full sandbox is the opposite, and it carries every production record. A Partial Copy sandbox falls between the two. Chapter on sandbox types and templates covers how a template chooses the objects and the sample size.
What this means for seeding
The copy behavior sets the main risks.
- Scratch orgs and Developer sandboxes: the risk is missing data. You seed them yourself.
- Partial and Full sandboxes: the risk is sensitive data. Production records, including personal information, now exist outside production. You mask or limit them. A later chapter covers this.
- Refreshed sandboxes: a refresh replaces the sandbox with a new copy. Records you added by hand or by script are lost, so seeding that must survive a refresh has to run again afterwards.
The diagram below shows the decision.
Common mistakes
- Importing before deploying. The import fails because the objects or fields are not yet in the org.
- Assuming a Developer sandbox has data. It has none, so tests that depend on records fail.
- Treating a Full sandbox as safe. It holds real customer data and needs the same care as production.
- Seeding once by hand. A refresh or a scratch org expiry removes the data, so automate the process.
Exercise
A teammate creates a scratch org, then runs sf data import tree before running sf project deploy start. The import fails. Why, and what is the correct order?
Show answer
A scratch org starts with no custom objects or fields from your project, so the import cannot find the fields it needs. Deploy the metadata first with sf project deploy start, then run the data import.
Exercise
Which sandbox types contain no production records after creation, and what should you do to get test data into them?
Show answer
Developer and Developer Pro sandboxes. Seed them yourself, for example with a CLI tree import or an Apex script.