Sub topic 7
Sandboxes and Their Types
This chapter assumes you know what an org is and that production is the live org where your users work. It also assumes you understand the difference between metadata (configuration and code) and data (records). A sandbox is a copy of a production org that you use for development, testing, or training. You can work in it without touching live data or live users.
What a sandbox is
A sandbox is a separate org that is created from a production org. It has its own Org ID, its own URL, and its own users and logins. Changes you make in it do not affect production.
Three facts define how sandboxes behave:
- A sandbox always copies the metadata of its source org. This includes custom objects, fields, Apex classes, flows, page layouts, and permission sets.
- How much data it copies depends on the sandbox type.
- A sandbox has the same edition as the production org it came from. You cannot create a sandbox from a Developer Edition or trial org.
Sandboxes are not a backup. A sandbox is a snapshot at the moment it was created or last refreshed, and it does not stay in sync with production afterward.
The four sandbox types
Salesforce offers four types. They differ in how much data they copy, how much storage they have, and how often you can refresh them.
| Type | Data copied | Data storage | File storage | Minimum refresh interval |
|---|---|---|---|---|
| Developer | None (metadata only) | 200 MB | 200 MB | 1 day |
| Developer Pro | None (metadata only) | 1 GB | 1 GB | 1 day |
| Partial Copy | Sample of records, chosen by a sandbox template | 5 GB | Same as production | 5 days |
| Full | All data, optionally including field history and Chatter | Same as production | Same as production | 29 days |
The limits in this table are the documented values at the time of writing. Salesforce changes them occasionally, so confirm them in the current Salesforce Help before you plan around them.
Developer sandbox
A Developer sandbox holds metadata only. It starts with no records, apart from a few that Salesforce creates automatically. It suits individual developers and administrators who build and unit test features. Because it is small and can be refreshed daily, teams often give each developer their own.
Developer Pro sandbox
A Developer Pro sandbox works like a Developer sandbox but has more storage. Use it when a developer or tester needs to load a larger set of test data, or when an integration test needs more room.
Partial Copy sandbox
A Partial Copy sandbox copies metadata plus a sample of production data. You decide which objects and how many records to copy by creating a sandbox template. Use it for quality assurance (QA), user acceptance testing, and integration testing, where realistic data matters but a full copy is unnecessary.
Full sandbox
A Full sandbox is a replica of production, including all records and attachments. Optionally it also includes field history tracking and Chatter data. Use it for performance testing, load testing, staging, and final rehearsals before a release. Its long refresh interval (29 days) means you cannot reset it often, so plan the refresh schedule.
How many sandboxes you get
The number and mix of sandboxes depend on the edition and on what you have purchased. Higher editions include more sandboxes. Professional Edition has a limited number of Developer sandboxes, and Enterprise Edition includes more of them, plus a Partial Copy sandbox. Unlimited and Performance Editions include a larger set that also contains a Full sandbox. You can buy extra sandboxes as add-ons.
Check your own allocation in Setup. Search for Sandboxes in Quick Find. The page lists each license type with the number used and the number available.
Creating a sandbox
You create a sandbox from the production org, either in Setup or with the Salesforce CLI. In Setup, open Sandboxes, click New Sandbox, give it a name, choose a type, and choose which production data to copy if the type allows it.
Creation is not instant. A Developer sandbox may take minutes. A Full sandbox with large data volumes can take hours or days. Salesforce emails you when the sandbox is ready.
The sandbox name has a limit of 10 characters. The name becomes part of every username and the sandbox URL.
With the CLI you describe the sandbox in a definition file:
{
"sandboxName": "qa",
"licenseType": "Developer"
}Save that as config/qa-sandbox-def.json. Then create the sandbox against your authorized production org:
sf org create sandbox --definition-file config/qa-sandbox-def.json --alias qa --target-org my-prodThe command waits for the sandbox to be ready by default. The --target-org flag must point to the production org that will be the source, not to the sandbox.
What changes in the copy
Salesforce changes some things during the copy so the sandbox does not behave like a second production org.
- Usernames: The sandbox name is appended to each username. The user
[email protected]becomes[email protected]. You log in with the new username, and your password is the same as in production at copy time. - Email addresses: Salesforce appends
.invalidto user email addresses by default. This stops test activity from emailing real people. The default Email deliverability setting is System email only, which also prevents outbound email to customers. - URLs: A sandbox gets its own My Domain URL in the form
mycompany--qa.sandbox.my.salesforce.com. - Scheduled jobs: Scheduled Apex jobs are not carried over as running jobs, and some automated processes are paused, so a copy does not send live traffic.
- Integrations: Named credentials, endpoints, and connected app settings can still point at production systems. You must repoint them to test systems after each copy.
A copy that still holds real customer data and real endpoints is a common source of accidents. Treat the first minutes after a refresh as a cleanup task.
Refreshing a sandbox
A refresh replaces a sandbox with a new copy of its source org. It keeps the sandbox name and type, but it discards everything in the sandbox: any metadata changes not deployed elsewhere, any test data you loaded, and any config you made directly there.
Before you refresh, make sure that all work you want to keep exists in version control or has been deployed to another org. After a refresh, the sandbox matches production again, so metadata deployed to production since the last refresh shows up automatically.
Each type has a minimum interval between refreshes (see the table above). If you try to refresh too soon, Setup keeps the button disabled until the interval has passed.
Automating setup after a copy
Doing the same cleanup after every refresh by hand wastes time and invites mistakes. Salesforce provides an Apex interface, SandboxPostCopy, which runs a class automatically after a sandbox is created or refreshed.
global class PrepareSandbox implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug('Production Org ID: ' + context.organizationId());
System.debug('Sandbox ID: ' + context.sandboxId());
System.debug('Sandbox name: ' + context.sandboxName());
// Add cleanup here: repoint settings, mask data, create test users.
}
}You select this class when you create or refresh the sandbox, in the Apex Class field of the creation wizard. The class must exist in production before you create the copy, because the copy inherits it from production.
Telling a sandbox from production
When you are working in an org, you can check which one you are in. The Organization object has an IsSandbox field:
SELECT Id, Name, OrganizationType, IsSandbox, InstanceName FROM OrganizationIf IsSandbox is true, you are in a sandbox. The Org ID returned is the sandbox's own ID, which differs from the production Org ID. Code that must behave differently in a sandbox, such as code that avoids sending real notifications, can use this check. In Apex you can read the same value with [SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox.
Sandboxes versus other org types
Sandboxes are not the same as scratch orgs or Developer Edition orgs.
- A sandbox is copied from production. It reflects your real configuration and (depending on type) your real data.
- A scratch org is built from a definition file. It starts empty and is meant to be disposable, which the next chapter covers.
- A Developer Edition org is independent of production. It has no link to any production org.
Choose a sandbox when you need to test against your real configuration, data shape, and integrations. Choose a scratch org when you want a clean, reproducible build from source code.
Common mistakes
- Choosing the wrong type. Testing a data-heavy batch job in a Developer sandbox fails because the data is not there. Match the type to the test.
- Refreshing before saving work. A refresh erases un-deployed changes.
- Forgetting integrations. A sandbox that still calls production endpoints can alter real systems.
- Planning around a Full sandbox refresh. With a 29-day interval, a missed refresh window can delay a release by weeks.
- Using the production login URL. Log into a sandbox through
test.salesforce.comor its own My Domain URL, with the sandbox username.
Exercise
Your team must run a performance test with realistic data volumes before a release, and a second team needs a small environment for developers to build features with no production records. Which sandbox type fits each need, and why?
Show answer
Use a Full sandbox for the performance test, because it copies all production data and has the same storage as production, so volumes and query behavior are realistic. Use Developer sandboxes (or Developer Pro if more storage is needed) for the developers, because they copy metadata only and can be refreshed daily.
Exercise
A developer refreshes a Developer sandbox and finds that a flow they edited directly in the sandbox yesterday is gone. What happened, and how could the team avoid it?
Show answer
A refresh replaces the sandbox with a fresh copy of production, discarding all changes made in the sandbox. The team can avoid this by keeping changes in version control or deploying them to another org before refreshing.