Sub topic 10
Cloning a Sandbox from Another Sandbox
This chapter assumes you know the four sandbox types, their storage limits, and how to create a sandbox from Setup. It covers what a clone is, when to use one, and what to check afterwards.
What cloning means
Cloning creates a new sandbox whose source is an existing sandbox instead of production. The new sandbox gets the source's metadata, such as objects, fields, flows, Apex, and permission sets, as it exists at the moment of the copy. For types that copy data, it also gets the source's records, up to the storage limit of the new sandbox.
The source can contain work that has never reached production. A clone captures that work, and a sandbox created from production does not.
Creating from production and cloning compared
| Aspect | Created from production | Cloned from a sandbox |
|---|---|---|
| Source of metadata | Production | The source sandbox, including unreleased changes |
| Source of data | Production, according to the sandbox type or template | The source sandbox's records |
| Typical use | A clean copy of what is live | A copy of work in progress |
| Effect on the source | None | None, but the source is busy while it is copied |
| License consumed | One of the chosen type | One of the chosen type |
Cloning does not alter or lock the source for normal use. Salesforce does restrict some operations on the source while a copy is in progress, such as refreshing or deleting it. Plan around that and don't start the clone in the middle of a deployment.
When cloning is the right choice
Clone when the state you need exists only in a sandbox. These are common cases:
- Parallel feature work. A team has built the first half of a feature in one sandbox. A second developer needs the same baseline without waiting for a release to production.
- A stable test copy. QA needs a frozen copy of a sandbox so that testing is not disturbed by the developers still changing the original.
- Experimenting safely. You want to try a risky change, such as a data migration or a package upgrade, on a copy of a sandbox that is already configured.
- Recovering a configured environment. A sandbox took a long time to set up with test users, integrations, and sample data. Cloning duplicates that effort in minutes or hours.
Don't clone when you need a faithful picture of production. The clone carries the drift of the source: half-finished changes, test data, and altered settings. For a release rehearsal, create from production or refresh from production.
Creating a clone from Setup
- In the production org, open Setup and enter Sandboxes in Quick Find.
- Click New Sandbox.
- Enter a name and a description. Sandbox names are short and alphanumeric. Because the name is appended to usernames, shorter names make logins easier to type.
- Choose the sandbox type. The license for that type must be available, and the type decides how much data can be copied.
- In the source selection step, choose an existing sandbox as the source instead of production.
- If the type is Partial Copy or Full, choose the data options that apply. Check the Salesforce Help for your release to see which options are offered when the source is a sandbox.
- Optionally, choose a Apex class to run after the copy (see below), then click Create.
The sandbox shows a status of Pending, then Processing, then Completed. Salesforce sends an email when it is ready. The time depends on the amount of metadata and data. A large Full copy can take many hours, while a Developer clone is usually much quicker.
Choosing the type of the clone
The type you pick for the clone follows the same limits as any other sandbox:
- A Developer or Developer Pro clone can hold only as much data as its storage allows. A clone of a large Full sandbox into a Developer sandbox does not copy the data volume. Expect the metadata to come across, and expect data to be limited by what fits.
- A Partial Copy clone is bounded by its own storage and by the template or data options you choose.
- A Full clone has the same storage as production, so it can hold the whole source if the source is also a Full sandbox.
If the source holds more data than the clone's storage allows, the creation can fail or copy only part of the data, depending on the type. Always compare the source's data storage usage (Setup, Storage Usage) with the target type's limit before you start.
What to check after the clone finishes
A clone behaves like a freshly created sandbox in several respects, because Salesforce treats it as a new organization with a new org ID.
- Usernames. Usernames get the sandbox name appended, in the form
[email protected]. Log in with the new form. - Email deliverability. Email access is usually restricted in a new sandbox, so that test records do not email real customers. Check Setup, Deliverability, and set it deliberately.
- Integrations and endpoints. Named credentials, remote site settings, and connected app callbacks are copied as they were in the source. If the source pointed at a test endpoint, the clone points there too. If the source still pointed at a production service, fix that before anything runs.
- Scheduled jobs and batch processes. Check scheduled Apex and flows that call external systems before users log in. Duplicate jobs in two sandboxes calling the same external system can cause confusing results.
- Data that is stale. The records are as old as the source, not as old as production. If the source was last refreshed two months ago, the clone is two months behind production.
Automating the setup with SandboxPostCopy
A class that implements SandboxPostCopy can run after a sandbox is created, cloned, or refreshed. You choose the class while creating the sandbox. That makes it the right place to repeat the cleanup steps you would otherwise do by hand after every clone. The context tells the class which sandbox is being prepared.
global class PrepareClone implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug('Preparing sandbox: ' + context.sandboxName());
System.debug('Org ID: ' + context.organizationId());
System.debug('Sandbox ID: ' + context.sandboxId());
}
}The class must be in the source org, because the clone receives the source's Apex. If the class lives only in production and not in the source sandbox, the clone cannot find it. This is a common mistake, so confirm that the class is deployed to the source before you start.
You can also write code that behaves differently in a sandbox than in production by checking the Organization record.
Organization org = [SELECT Id, IsSandbox FROM Organization LIMIT 1];
if (org.IsSandbox) {
System.debug('Running in a sandbox');
} else {
System.debug('Running in production');
}Cloning with the Salesforce CLI
The CLI can create a clone as well. It takes the source sandbox name through the --clone flag and runs against the production org.
sf org create sandbox --name dev2 --license-type Developer --clone dev1 --alias dev2 --target-org prodHere dev1 is the source sandbox, dev2 is the new sandbox, and prod is the alias of the production org. Run sf org create sandbox --help to confirm the flags in your CLI version.
Common errors
- No license available. The clone needs a free license of its type. Delete an unused sandbox or choose a different type.
- Source busy. A refresh or another copy is running on the source. Wait for it to finish.
- Data too large. The source's data exceeds the target's storage. Choose a larger type or reduce the source data.
- Post-copy class not found. The Apex class is missing in the source sandbox. Deploy it there first.
- Surprise behavior from unreleased changes. The clone is not a copy of production. Compare it with production metadata before you rely on it for a release test.
Exercise
A team lead wants a second sandbox that includes a feature her developers built over the last three weeks in a Developer sandbox. The feature has not been deployed to production. Should she create the new sandbox from production or clone the Developer sandbox, and which one thing should she check before starting?
Show answer
She should clone the Developer sandbox, because a sandbox created from production would not contain the unreleased feature. Before starting she should confirm that the sandbox type she picks has a free license and enough storage for the source's data. If she uses a SandboxPostCopy class, she should also confirm that it is deployed in the source sandbox.
Exercise
You cloned a sandbox that was last refreshed from production six weeks ago. Name two ways the clone differs from a fresh sandbox created from production today.
Show answer
The data and metadata are six weeks behind production, and the clone also carries the source sandbox's unreleased changes and settings, such as integration endpoints and test data. A fresh sandbox from production would have neither the staleness nor the extra work in progress.