Sub topic 10
Sandbox Types and Sandbox Templates
This chapter explains what each sandbox type copies from production. It also covers how a sandbox template limits that copy, and how those choices affect your seeding plan. It assumes you know the Salesforce data model, can use the sf CLI against a production org, and have read the earlier chapter on how scratch orgs start empty.
Why the sandbox type decides your seeding strategy
A scratch org always starts with no data, so you load everything yourself. A sandbox is different. Salesforce builds it as a copy of a production org, and how much data arrives depends on the sandbox type you pick. Before you write any seeding script, find out what the sandbox already contains. Otherwise you may load records that duplicate copied ones, or find that the data you expected is missing.
The four sandbox types
| Type | Metadata | Data copied | Typical data storage limit | Minimum refresh interval |
|---|---|---|---|---|
| Developer | Full copy | None | 200 MB | 1 day |
| Developer Pro | Full copy | None | 1 GB | 1 day |
| Partial Copy | Full copy | A sample, chosen by a template | 5 GB | 5 days |
| Full | Full copy | All data, optionally with history | Same as production | 29 days |
The storage and refresh figures come from Salesforce's published limits. They can change between releases, so check the current documentation before you plan around them.
Two points are easy to miss:
- Developer and Developer Pro sandboxes copy metadata only. Everything in them comes from your seed process, so they behave much like scratch orgs. They live longer, and they hold your org's configuration as it exists in production.
- Partial Copy sandboxes copy a sample, not a filtered subset you control record by record. You control which objects are included. You do not control which records of those objects are picked.
What a sandbox template is
A sandbox template is a saved list of objects. When you create or refresh a Partial Copy or Full sandbox, you can attach a template to it. The template tells Salesforce which objects' records to copy. Metadata is copied either way.
Key facts about templates:
- Templates apply only to Partial Copy and Full sandboxes. Developer and Developer Pro sandboxes ignore them because they copy no data.
- You manage templates in Setup under Sandbox Templates. A template is created in production and can then be reused across sandboxes.
- You select objects. For Partial Copy, Salesforce copies a sample of records for each selected object. The documented cap is 10,000 records per object.
- When you add an object, Salesforce can require its parent objects too. This keeps lookup and master-detail relationships from pointing at records that were not copied.
- Editing a template does not change a sandbox that already exists. The new selection applies the next time you create or refresh a sandbox with that template.
A template is a blunt tool. It answers "which objects?" but not "which records?" If you need a specific Account with exactly five Contacts, a template cannot guarantee that. Plan to add those records with a seed script afterwards.
Designing a template
Start from the object list in your seed design, the one you built when you mapped dependencies. Then:
- Include the objects your tests and demos actually use, such as Account, Contact, Opportunity, and any custom objects that hang off them.
- Leave out large, low-value objects such as logs, history, and attachments. They consume sample space and storage and add little.
- Check which related objects Salesforce adds automatically. Remove any you do not need, if the tool allows it.
- Name the template for its purpose, for example
QA-Core-Sales, so people pick the right one at refresh time.
Creating a sandbox from the CLI
You can create a sandbox with the CLI instead of Setup. Run the command against production, using a definition file for the sandbox. The sandbox name must be alphanumeric and at most 10 characters.
{
"sandboxName": "qa1",
"licenseType": "Partial"
}Save this as config/sandbox-def.json. Valid licenseType values include Developer, Developer_Pro, Partial, and Full. Then run:
sf org create sandbox \
--definition-file config/sandbox-def.json \
--alias qa1 \
--target-org my-production-org \
--wait 60The --target-org value is the production org that owns the sandbox. The --alias value is the alias you will use for the new sandbox once it is created. Sandbox creation can take minutes to hours, depending on type and size. If the wait time runs out, the CLI tells you how to resume.
The definition file can also name a template and a post-copy Apex class. Check sf org create sandbox --help and the SandboxInfo documentation for the exact property names in your CLI version. The next chapter covers the post-copy class.
What changes in a copied sandbox
Copied data is not identical to production. Be aware of these behaviors:
- User email addresses are changed. Salesforce appends
.invalidto user emails in a sandbox so test activity does not send real mail. Contact and Lead emails are not changed by default, so you must handle them yourself. That is a sensitive-data concern, covered in a later chapter. - Record IDs are preserved for copied records. An Account copied into the sandbox keeps its production ID. This helps for debugging, but never hard-code IDs in seed scripts. IDs differ between orgs, and a record you create yourself gets a new ID.
- Scheduled jobs and integrations may be inactive or point to production. Review named credentials and endpoint settings after every refresh.
- Files, Chatter, and field history are optional or limited, depending on the type and the options you choose.
Combining templates with seed scripts
In practice, a Partial Copy sandbox uses two layers:
- Template layer. It provides bulk background data: enough Accounts and Opportunities to make reports and list views look realistic.
- Seed script layer. It adds the exact records tests depend on, using the upsert pattern from the earlier chapter on external IDs.
The seed layer must tolerate whatever the template produced. Because the template sample is not under your control, do not assume an empty table. Key your records on an external ID field and upsert them. A second run then updates your records and leaves the sampled ones alone.
sf data upsert bulk \
--sobject Account \
--file data/accounts.csv \
--external-id External_Id__c \
--target-org qa1 \
--wait 10The external ID field must exist in the sandbox. If your metadata is deployed from source control, deploy it before you load data. A sandbox copied from production already has the production metadata, but a field added only in a feature branch will be missing.
Common mistakes
- Expecting data in a Developer sandbox. It arrives empty, so a seed script that assumes "copy of production" finds no rows.
- Relying on a specific sampled record. A refresh can pick different records, so tests that depend on a particular Account break after the next refresh.
- Forgetting the refresh interval. A Full sandbox can be refreshed only about once a month. Do not build a daily reset process around it. Use a seed script that cleans and reloads data inside the existing sandbox instead.
- Editing a template and expecting the sandbox to change. The edit takes effect only at the next refresh.
- Hard-coded record IDs in scripts. They work in one org and fail in the next.
Exercise
Your team uses a Developer Pro sandbox and wants a sandbox template so that 10,000 sample Accounts appear after each refresh. Will this work? If not, what should they do?
Show answer
No. Templates apply only to Partial Copy and Full sandboxes. A Developer Pro sandbox copies metadata only, so it ignores the template. The team has two options. They can switch to a Partial Copy sandbox and attach the template, or they can keep the Developer Pro sandbox and load Accounts with a seed script, such as a CLI tree import or a bulk upsert keyed on an external ID.
Exercise
A Partial Copy sandbox is refreshed, and a test that relied on a particular sampled Account now fails. What is the underlying cause, and how do you fix the setup?
Show answer
Partial Copy sandboxes copy a sample of records for each object in the template, and you cannot choose which records. A refresh can select a different sample. Fix this by creating the required Account in a seed script that upserts on an external ID, and make the test use that record instead of a sampled one.