Sub topic 16
Creating a Sandbox with the Salesforce CLI
This chapter assumes you have the Salesforce CLI (sf, version 2) installed. It also assumes you can authorize a production org with sf org login web and that you know the sandbox types from earlier chapters. You need a production org with sandbox licenses available. Developer sandboxes are the cheapest to try.
Why create sandboxes from the command line
Creating a sandbox in Setup is fine for a one-off. The CLI is better when you want repeatable results. A definition file lives in version control, so a teammate or a CI job creates the same sandbox with the same name, type, and post-copy class. It also makes the choices (type, template, post-copy class) reviewable in a pull request.
The CLI call creates the same kind of sandbox as Setup. The sandbox counts against the same license allotment and has the same storage limits and refresh interval for its type.
Authorize the source org
The command runs against an org that already exists. For a new sandbox, that is production. Log in once and give the org an alias:
sf org login web --alias prodYou need a user with the Manage Sandboxes permission, which System Administrators have by default.
The definition file
The definition file is JSON. Two properties are enough to create a sandbox:
{
"sandboxName": "dev1",
"licenseType": "Developer"
}Save it as config/sandbox-def.json.
sandboxNameis the sandbox name. It must be alphanumeric and at most 10 characters. The name becomes part of every username in the sandbox.licenseTypesets the sandbox type.
Accepted licenseType values:
| Value | Sandbox type |
|---|---|
| `Developer` | Developer |
| `Developer_Pro` | Developer Pro |
| `Partial` | Partial Copy |
| `Full` | Full |
You can add optional properties. These are the ones you will use most:
{
"sandboxName": "qa1",
"licenseType": "Partial",
"description": "QA regression testing for the Q4 release",
"templateId": "0EM000000000001AAA",
"apexClassId": "01p000000000001AAA"
}descriptionis free text shown in Setup.templateIdis the ID of a sandbox template. It applies to Partial Copy and Full sandboxes, where the template controls which objects and data are copied.apexClassIdis the ID of aSandboxPostCopyclass. Salesforce runs it after the sandbox is created or refreshed.
The two IDs above are placeholders. Replace them with real 15 or 18 character IDs from your org. You can find them in Setup, or query the class:
sf data query --query "SELECT Id, Name FROM ApexClass WHERE Name = 'PrepareSandbox'" --target-org prodThe post-copy class must already exist in production, because a new sandbox copies metadata from production. Deploy it there first, or the ID will not resolve.
Run the create command
sf org create sandbox --definition-file config/sandbox-def.json --alias dev1 --target-org prodEach flag has a job:
--definition-file(-f) points to the JSON file.--alias(-a) is the local nickname the CLI uses for the new org. It is separate from the sandbox name.--target-org(-o) is the org that owns the sandbox license. For a normal sandbox, that is production.
You can skip the file for simple cases and pass values as flags:
sf org create sandbox --name dev1 --license-type Developer --alias dev1 --target-org prodUse a flag-only command for quick experiments. Use a file when the setup should be shared or repeated.
What happens while it runs
Creating a sandbox is asynchronous. The CLI submits a request, then polls Salesforce until the sandbox is ready. Copying a Developer sandbox can take minutes. Partial Copy and Full sandboxes can take hours or longer, depending on data volume.
The wait period matters. By default the CLI waits up to 30 minutes. Control it with --wait (minutes) and --poll-interval (seconds):
sf org create sandbox -f config/sandbox-def.json -a qa1 -o prod --wait 60 --poll-interval 60If the wait ends before the sandbox is ready, the command stops waiting. It does not cancel the sandbox. Salesforce keeps building it. You then have two options.
Resume polling later:
sf org resume sandbox --name qa1 --wait 120Or skip waiting from the start and check back:
sf org create sandbox -f config/sandbox-def.json -a qa1 -o prod --asyncWith --async, the command returns right away. Run sf org resume sandbox when you are ready to finish the setup.
Verify and open the sandbox
When the command finishes, the sandbox is authorized under your alias. Confirm it:
sf org list
sf org display --target-org dev1
sf org open --target-org dev1The username follows the pattern production.username.sandboxname. For a production user [email protected] and sandbox dev1, the sandbox username is [email protected]. This is the same renaming behavior described in the chapter on what changes after a refresh.
To confirm that you are in a sandbox from code, use the Organization query from earlier chapters:
sf data query --query "SELECT Id, IsSandbox FROM Organization" --target-org dev1IsSandbox should be true.
Creating a sandbox from another sandbox
The same command clones a sandbox. Point --clone at the source sandbox name and set --target-org to production, which still owns the licenses:
sf org create sandbox --clone dev1 --name dev2 --alias dev2 --target-org prodClone from a sandbox when you want its configuration and data as they are now, rather than as they are in production. The new sandbox has the same type limits and name rules as any other.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Name rejected | Name is longer than 10 characters or has symbols | Use a shorter alphanumeric name |
| No license available | All licenses of that type are in use | Delete an unused sandbox or choose another type |
| Name already in use | A sandbox with that name exists, or was deleted recently | Choose another name, or wait for the deletion to finish |
| Unknown `apexClassId` | The class is not in production, or the ID is wrong | Deploy the class to production and re-query the ID |
| Command timed out | `--wait` ended before the copy finished | Run `sf org resume sandbox --name <name>` |
| Not authorized | The `--target-org` alias is not logged in | Run `sf org login web --alias prod` again |
Delete a sandbox you no longer need
Free the license when you are done:
sf org delete sandbox --target-org dev1 --no-promptDeletion is permanent for the sandbox and its data. Omit --no-prompt to get a confirmation question first.
Exercise
You run sf org create sandbox -f config/sandbox-def.json -a qa1 -o prod for a Full sandbox, and the command exits after 30 minutes with the sandbox still copying. Did the creation fail, and what do you run next?
Show answer
It did not fail. The default wait ended, but Salesforce keeps building the sandbox. Run sf org resume sandbox --name qa1 --wait 120 (using the sandbox name from the definition file) to keep polling. When the sandbox completes, the CLI authorizes it under the alias.
Exercise
Write a definition file that creates a Developer Pro sandbox named dev2 and runs a post-copy Apex class after the copy. What must be true of the class before you run the create command?
Show answer
{"sandboxName": "dev2", "licenseType": "Developer_Pro", "apexClassId": "<class ID>"}. The class must implement SandboxPostCopy and already exist in production, because the new sandbox copies its metadata from production, so you can look up the ID there.