Sub topic 3
Creating and refreshing sandboxes
This chapter assumes you know the four sandbox types from the previous chapter. It also assumes you can run the Salesforce CLI (sf) and have authorized a production org. Here you will create a sandbox in the CLI, learn how refresh works, and see what to prepare for it.
What creation and refresh do
A sandbox is a copy of a source org, usually production. Creating one copies metadata, and for Partial Copy and Full sandboxes it also copies data. A refresh replaces the sandbox with a new copy of the source. Changes you made in the sandbox are lost, including undeployed metadata, test records, and user settings.
Refresh is therefore the main way to fix a sandbox that has drifted from production. It is also the main way to lose work, so deploy or commit everything first.
Refresh intervals
Each type has a minimum wait between refreshes, counted from the last creation or refresh.
| Sandbox type | Refresh interval | Data copied |
|---|---|---|
| Developer | 1 day | Metadata only |
| Developer Pro | 1 day | Metadata only |
| Partial Copy | 5 days | Metadata plus a sample of data chosen by a sandbox template |
| Full | 29 days | Metadata and all data |
Check your org's current limits in Setup, because editions and contracts affect how many sandboxes of each type you can have.
Create a sandbox with a definition file
The CLI reads a sandbox definition file. This one requests a Developer sandbox named dev1:
{
"sandboxName": "dev1",
"licenseType": "Developer"
}Save it as config/dev-sandbox-def.json. Then create the sandbox, using production (aliased prod) as the source:
sf org create sandbox --definition-file config/dev-sandbox-def.json --alias dev1 --target-org prodTwo details cause most errors here:
--target-orgis the source org that owns the sandbox, not the new sandbox.sandboxNamemust be short (10 characters or fewer) and alphanumeric. Names are unique within the production org.
Creation can take minutes for a Developer sandbox and hours or days for a Full one. The CLI waits for a limited time, then stops. If it stops, the sandbox keeps building on Salesforce's side, and you can reconnect with sf org resume sandbox.
After creation, the CLI stores the sandbox as an authorized org under your alias. Sandbox usernames take the production username plus the sandbox name as a suffix, for example [email protected].
Refreshing a sandbox
You start a refresh from Setup, on the Sandboxes page, by choosing Refresh next to the sandbox. You pick the license type again and can pick a different source. The sandbox is rebuilt from the source org's current state. Refreshing a Partial Copy sandbox also asks for its sandbox template, which controls which objects and how many records are copied.
After a refresh, the CLI's stored authorization may no longer work, and users must log in again. Test users and other copied users keep their usernames, but their email addresses are altered so the sandbox does not send mail to real people. Do not assume integrations still work, because endpoints, named credentials, and connected app settings may need adjusting.
Plan the work around refresh
- Keep all metadata changes in version control, not only in the sandbox. A refresh then costs you nothing.
- Announce refresh dates to the team so nobody loses unsaved work.
- Automate setup after the copy. A class that implements
SandboxPostCopyruns after each create or refresh. It can adjust settings, seed records, or mask data. The post-copy chapter covers this in detail. - Delete sandboxes you no longer use with
sf org delete sandbox --target-org dev1. They count against your allocation.
Exercise
A developer wants to start a Full sandbox refresh on day 10 after its last refresh. Is that allowed? What should they do first?
Show answer
No. A Full sandbox can be refreshed only once every 29 days, so the refresh must wait until day 29 or later. Before any refresh, they should deploy or commit all work from the sandbox to another org or source control, because the refresh replaces everything in it.