Sub topic 16
Creating a Sandbox with the Salesforce CLI
This chapter assumes you know the four sandbox types, can create a sandbox from Setup, and have the Salesforce CLI (sf) installed. You create the same sandbox from the command line that you would create in Setup. The CLI is useful when you want the step in a script or a repeatable runbook.
What You Need First
- The
sfCLI, version 2 or later. - Production (or the source sandbox) authorized in the CLI with an alias. This chapter uses
prod. - A user with permission to create sandboxes in that org.
- Enough sandbox licenses for the type you request.
To authorize production, run sf org login web --alias prod.
The Definition File
A definition file is a JSON file that describes the sandbox. Two properties matter most:
sandboxNameis the name of the sandbox. It must be unique in the org and is limited to 10 alphanumeric characters.licenseTypesets the sandbox type. Accepted values areDeveloper,Developer_Pro,Partial, andFull.
{
"sandboxName": "dev1",
"licenseType": "Developer"
}Save this as config/sandbox-def.json. The license type decides the storage limit, the refresh interval, and which data is copied, as covered in earlier chapters.
You can also add a templateId property to apply a sandbox template. Templates apply to Partial Copy and Full sandboxes only.
Running the Command
sf org create sandbox --definition-file config/sandbox-def.json --alias dev1 --target-org prod--definition-filepoints to the JSON file.--aliasis the local nickname the CLI gives the new sandbox.--target-orgis the org that owns the sandbox license. Here it is production.
You can also skip the file and pass values as flags, such as --name dev1 --license-type Developer.
By default the command waits for the sandbox to finish. Creation can take minutes for a Developer sandbox, and hours or more for a large Full sandbox. If the wait times out, the CLI keeps going in Salesforce, and you can check progress later with sf org resume sandbox.
Common Errors
| Symptom | Likely cause |
|---|---|
| No authorization found for `prod` | The alias is wrong, or you have not run `sf org login web`. |
| License limit error | All licenses for that type are in use. Delete a sandbox or choose another type. |
| Name rejected | The name is too long, contains symbols, or is still in use by a sandbox being deleted. |
| Command times out | The sandbox is still building. Resume it instead of creating it again. |
Connecting the Pieces
A new sandbox has the production metadata and the data its type copies. Any post-copy setup still has to run. If you named a SandboxPostCopy class when you created the sandbox, Salesforce runs it after the copy finishes.
Exercise
You need a Full sandbox named qa2 from production, aliased as qa2. Production is authorized with the alias prod. What do you put in the definition file, and what command do you run?
Show answer
The file contains {"sandboxName": "qa2", "licenseType": "Full"}. Run: sf org create sandbox --definition-file config/sandbox-def.json --alias qa2 --target-org prod