Sub topic 9
Creating a sandbox from Setup
This chapter assumes you can open Setup in a production org and know how the four sandbox types differ in data copied, storage, and refresh interval. It covers the New Sandbox wizard, the choices you make in it, and how to confirm the result.
Where the wizard lives
Sandboxes are created from the production org, or from another sandbox when you clone. In Setup, enter Sandboxes in Quick Find and open Environments > Sandboxes. The page shows your sandbox licenses and a list of existing sandboxes. Click New Sandbox to start.
You need permission to manage sandboxes. In most orgs, System Administrators have it. If the button is missing, check your permissions first, then your licenses.
The list page also shows how many sandboxes of each type you can still create. If a type's count is zero, that type is greyed out in the wizard. You must delete a sandbox of that type, or buy another license, before you can create one.
The wizard in order
The wizard asks for the following, in this order.
- Name. Use up to 10 alphanumeric characters. The name becomes part of every username in the sandbox, so pick something short and stable such as
dev1orqa. You cannot rename a sandbox later. - Description. Free text. Use it to record the purpose and owner, because a list of sandboxes with names like
test2becomes hard to read quickly. - Type. Developer, Developer Pro, Partial Copy, or Full. Only types you have licenses for are selectable.
- Source. Production, or an existing sandbox if you are cloning. Cloning is covered in its own chapter.
- Data options. These depend on the type, as shown in the next section.
- Apex class. An optional
SandboxPostCopyclass that runs after the copy finishes. - Activation option. Whether the sandbox activates automatically when it is ready.
Data options by type
The type you pick decides which data questions appear.
| Type | What you choose | What is copied |
|---|---|---|
| Developer | Nothing about data | All metadata, no records |
| Developer Pro | Nothing about data | All metadata, no records |
| Partial Copy | A sandbox template (required) | All metadata plus a sample of records defined by the template |
| Full | All data, or a template. Also how much field tracking history to include | All metadata plus records, optionally filtered by a template |
Two details trip people up.
- Partial Copy needs a template. Without one, the wizard will not let you continue. Create the template before you start the wizard. Templates are covered in the previous chapter.
- Full sandboxes copy more than records. You can also choose how much field history and Chatter data comes along. More history means a longer copy, so copy only what your testing needs.
Developer and Developer Pro sandboxes are limited by storage (about 200 MB and 1 GB of data). Metadata is copied in full, but any records you want must be loaded afterward. Plan to load test data with Data Loader, a script, or a post-copy class.
The Apex class selector
The optional Apex class field lists classes in the source org that implement SandboxPostCopy. After the copy finishes, Salesforce runs runApexClass once, as part of the creation. The class must exist in the source org before you click Create. If you forget it, you cannot add it to a sandbox that is already being created.
global class PrepareSandbox implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug('Org ID: ' + context.organizationId());
System.debug('Sandbox ID: ' + context.sandboxId());
System.debug('Sandbox name: ' + context.sandboxName());
}
}The SandboxContext argument tells the class which sandbox is being prepared. Use it to branch by sandbox name, for example to load different data into dev1 and qa. The class must be global, as shown. Keep it fast and make it safe to run more than once, because the same class is reused on every refresh.
Starting the copy and waiting
Click Create. The sandbox appears in the list with the status Pending, then Processing, then Completed. The wait depends on the type and the size of the source.
- Developer and Developer Pro sandboxes usually finish fastest, because only metadata is copied.
- Partial Copy sandboxes take longer, depending on the template.
- Full sandboxes can take hours or days for large orgs.
Salesforce emails the user who started the process when the sandbox is ready. If the email does not arrive, refresh the Sandboxes page and look at the status. The email is a convenience, not the source of truth.
If the sandbox does not activate automatically, the list shows a Login link. You must activate it within a limited number of days, or Salesforce deletes it. Activating it early avoids losing a long-running copy.
Logging in for the first time
Each sandbox has its own login. Salesforce changes usernames during the copy by appending the sandbox name. A production user [email protected] becomes [email protected] in the sandbox. Passwords are not reset by a creation, and your production password works with the new username. Sandbox users log in at https://test.salesforce.com, or through the My Domain URL for the sandbox if your org has one.
After logging in, check that you are in a sandbox before you do anything risky. This query reads the IsSandbox field of the Organization object.
Organization org = [SELECT Id, IsSandbox FROM Organization LIMIT 1];
if (org.IsSandbox) {
System.debug('Running in a sandbox');
} else {
System.debug('Running in production');
}Run it in the Developer Console's anonymous Apex window and read the result in the debug log. The same check is useful inside triggers or integration code, where you want different behavior in a sandbox, such as not calling a live payment endpoint.
Also check the following right away.
- The banner or org name matches the sandbox you meant to open.
- Email deliverability is set to System email only, which is the default for new sandboxes. This prevents test records from mailing real customers.
- Integration endpoints and named credentials still point at production until you change them. Review them before you run anything that calls out.
Common errors and how to avoid them
- Using a name that clashes with the username convention. Sandbox names longer than 10 characters, or with spaces or symbols, are rejected. Keep names short and lowercase.
- Creating a Partial Copy without a template. The wizard stops. Build the template first.
- Selecting a type with no free licenses. The option is disabled. Check the counts on the Sandboxes page.
- Relying on records in a Developer sandbox. There are none. Tests that need data must create it.
- Leaving the sandbox unactivated. A completed but never-activated sandbox can be removed after the activation window.
- Forgetting the post-copy class. It must be chosen at creation. Adding it later means refreshing.
For repeatable creation, the same choices can be written as a definition file and run from the Salesforce CLI. That approach has its own chapter.
Exercise
A teammate starts the New Sandbox wizard, picks Partial Copy, and finds they cannot continue. What is the likely cause, and what should they do?
Show answer
Partial Copy requires a sandbox template, and none is selected or none exists. They should leave the wizard, create a template that lists the objects to copy in the source org, then restart the wizard and choose that template.
Exercise
You create a Developer sandbox named dev1 from production. Your production username is [email protected]. What username do you use to log in, and what data should you expect to find?
Show answer
The username is [email protected], because Salesforce appends the sandbox name. You should expect all of production's metadata and no records, because Developer sandboxes copy configuration only.