Sub topic 2
Scratch orgs and sandboxes: what starts empty and what is copied
This chapter assumes you know the Salesforce data model (objects, fields, lookup and master-detail relationships) and that the sf CLI is installed and authorized to a Dev Hub or a production org. It also assumes you can read a Salesforce DX project layout and a small JSON file. By the end you will know what data each environment starts with. That tells you which seeding approach to pick in the later chapters.
Why the starting state matters
Seeding means getting your org into a known data state. Before you write any seed script, you need to know what the org already contains. If you load records into an org that already has data, you can create duplicates or hit uniqueness errors. If you assume data exists and it does not, your tests fail for reasons unrelated to your code.
The two environments you will use most differ in this respect:
- A scratch org is a disposable org you create from a definition file. It is built from your source code, so it starts without your business data.
- A sandbox is a copy of a production org (or of another sandbox). What it contains depends on the sandbox type, and some types copy real customer data.
What a scratch org starts with
A scratch org is created from a Dev Hub. You describe its shape in a definition file, usually config/project-scratch-def.json.
{
"orgName": "Seed Demo",
"edition": "Developer",
"hasSampleData": false
}Creating it from that file looks like this:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias my-scratch \
--set-default \
--duration-days 7A new scratch org has three properties that matter for seeding.
- It has no metadata from your project. Your custom objects, fields, and validation rules do not exist until you run
sf project deploy start(orsf project deployequivalents in your workflow). You cannot import records into a custom object that has not been deployed yet. - It has no business data. The
hasSampleDataoption controls whether the org is created with Salesforce's small set of sample records. It defaults to false, so the org is essentially empty apart from the admin user and system records. - It expires. The duration is 1 to 30 days, and the default is 7. When it expires, all of its data goes with it, so seeding must be repeatable. You will run it often.
You can confirm the starting state with a query:
sf data query \
--query "SELECT COUNT() FROM Account" \
--target-org my-scratchOn a fresh scratch org with hasSampleData set to false, expect a count of 0.
What a sandbox starts with
A sandbox is created from a production org, so its metadata always mirrors the source org. All sandbox types copy metadata. They differ in how much data they copy.
| Sandbox type | Data copied at creation | Typical use |
|---|---|---|
| Developer | None. Metadata only. | Individual development and unit tests |
| Developer Pro | None. Metadata only, with more storage than Developer. | Development with larger seed datasets |
| Partial Copy | A sample of data, defined by a sandbox template, with a cap per object | Integration testing and QA with realistic records |
| Full | A complete copy of production data, optionally limited by a template | Performance testing, staging, user acceptance testing |
Limits and refresh intervals change between releases, so check the current Salesforce documentation before you plan around them. The general pattern holds. Developer and Developer Pro sandboxes can be refreshed often (daily). Partial Copy sandboxes refresh less often, and Full sandboxes the least often (about once a month).
Two consequences follow.
- Developer and Developer Pro sandboxes behave like scratch orgs for seeding. They start with metadata and no data. You seed them with the same CLI commands you use for scratch orgs. The difference is that they last until you refresh or delete them, and they carry whatever configuration production has.
- Partial Copy and Full sandboxes start with data you did not choose. That data comes from production, so it can contain personal or confidential information. The chapter on sensitive data covers how to handle it. For now, remember that a copied dataset is a risk as well as a convenience.
What else changes in a sandbox
Even a data-free sandbox is not identical to production. Salesforce changes some values when it copies the org.
- Usernames get the sandbox name appended, for example
[email protected]. - User email addresses get
.invalidappended, so the sandbox cannot send mail to real people by accident. - Email deliverability is set to system email only after a refresh.
If your seed data creates users or contacts, plan for these rules. Do not hard-code production usernames in your scripts.
Comparing the environments for seeding
The table below summarizes what you need to plan for each environment.
| Question | Scratch org | Developer or Developer Pro sandbox | Partial Copy or Full sandbox |
|---|---|---|---|
| Starts with data? | No (unless `hasSampleData` is true) | No | Yes, from production |
| Starts with metadata? | Only what you deploy | Copy of production | Copy of production |
| Lifetime | 1 to 30 days | Until deleted or refreshed | Until deleted or refreshed |
| Created by | Dev Hub and a definition file | Production org (sandbox definition or Setup) | Production org (sandbox definition or Setup) |
| Seeding risk | Missing metadata | Duplicates after repeated loads | Duplicates and sensitive data |
| Main seeding tools | CLI tree import, bulk upsert, Apex | CLI tree import, bulk upsert, Apex, post-copy class | Templates, post-copy class, masking |
You can create a sandbox from the CLI too. The definition file names the sandbox and its type:
{
"sandboxName": "qa1",
"licenseType": "Developer"
}sf org create sandbox \
--definition-file config/sandbox-def.json \
--alias qa1 \
--target-org my-prod-org \
--wait 30Valid licenseType values include Developer, Developer_Pro, Partial, and Full. A sandbox can take from minutes to hours to create, depending on type and data volume. The --wait flag sets how many minutes the command waits before it returns.
Choosing where each step belongs
The diagram shows how the starting state leads to a seeding step.
This gives you a working rule for each environment.
- Scratch org: deploy first, then seed. Seeding without metadata fails.
- Developer sandbox: metadata already exists, so seed directly. Make the load safe to run twice, because the sandbox persists.
- Partial Copy or Full sandbox: inspect before you load. Use a template to limit what is copied, and mask or remove sensitive fields. Then add only the records that production does not provide.
Common mistakes
- Importing before deploying. In a scratch org, the import fails with an error about an unknown object or field. Run the deploy step first.
- Assuming a sandbox is empty. A Partial Copy or Full sandbox already has records. A seed script that inserts an Account named "Acme" can create a second "Acme" next to a copied one.
- Forgetting that refreshes erase data. Developers load test data once, refresh the sandbox, and lose it. Automate the load.
- Using production usernames. After a sandbox copy, usernames have a suffix. Look up users by a stable field, or create dedicated test users.
- Using a scratch org's
hasSampleDataas a substitute for design. Sample data is generic. Your tests need records that match your own data model, which the next chapters cover.
Exercise
You create a Developer sandbox from production and run a seed script that inserts 20 Accounts. A teammate refreshes the sandbox the next day and reports that the Accounts are gone. Separately, you create a Full sandbox and discover that an Account named "Acme" already exists. Explain both observations, and say what each tells you about how to design the seed process.
Show answer
A refresh replaces the sandbox with a fresh copy of production. A Developer sandbox copies metadata only, so the 20 Accounts you inserted are removed and the sandbox comes back with no data. The seed process must live in a script (or a post-copy Apex class) that can be rerun after every refresh. A Full sandbox copies production data, so "Acme" exists because it was copied from production. The seed process must not assume an empty org. It should check for existing records, for example by upserting on an external ID, so it does not create duplicates. It should also treat the copied data as potentially sensitive.