Sub topic 16
Org shape and scratch org snapshots
The earlier chapters created scratch orgs from a definition file that you wrote by hand. That works when you know which features and settings your project needs. It gets tedious when you want to match a complicated production org, or when every new scratch org needs the same slow setup. Salesforce offers two tools for these cases: org shape and scratch org snapshots.
This chapter assumes you can already authorize a Dev Hub, write a project-scratch-def.json file, and create and delete a scratch org with the sf CLI. It also assumes you have admin access to the org you want to model.
Two problems, two tools
The tools solve different problems.
- Org shape copies the configuration of an existing org, such as production or a sandbox. It answers the question "How do I get a scratch org with the same edition, features, limits, and settings as that org?"
- A scratch org snapshot copies the state of a scratch org. That includes its metadata, installed packages, and data. It answers the question "How do I skip the setup I repeat for every new scratch org?"
| Question | Org shape | Scratch org snapshot |
|---|---|---|
| What is the source? | A production org or sandbox | A scratch org created from your Dev Hub |
| What does it capture? | Edition, features, licenses, limits, settings | Metadata, data, installed packages, and settings |
| Where does it live? | In the source org | In the Dev Hub |
| How do you use it? | `sourceOrg` in the definition file, or `--source-org` | `--snapshot` when creating a scratch org |
Org shape
What a shape contains
A shape is a record of how an org is configured. It holds the edition, the enabled features, the licenses, the limits, and the org settings. It does not hold your custom objects, Apex classes, flows, or any data. You still deploy your source after the scratch org exists.
A shape is useful when a bug or a deployment failure only happens in an org with a certain feature set. A scratch org built from a shape of that org removes the "it works in my scratch org" problem. It does not remove the problem entirely, because the metadata and data are still yours to supply.
Enable and create a shape
Enabling org shape is a one-time step in the source org, not in the Dev Hub. In Setup, search for Org Shape in Quick Find and turn on the setting. You must be able to log in to the source org as an administrator. Authorize that org with the CLI, then create the shape:
sf org login web --alias my-prod
sf org create shape --target-org my-prod
sf org list shapesf org create shape returns a shape ID, which starts with 3SR. Shape creation is asynchronous, so the shape may not be ready for a few minutes. sf org list shape shows each shape with its source org ID and its status. Wait until the status is Active before you use it.
You also need the 15- or 18-character ID of the source org. It starts with 00D. The command sf org display --target-org my-prod prints it as Id.
Create a scratch org from a shape
There are two ways to point a scratch org at a shape. The first is a flag:
sf org create scratch --source-org 00D000000000000AAA --alias shape-scratch --duration-days 7 --target-dev-hub my-hubThe second is a sourceOrg key in the definition file, which suits a project that always builds from the same shape:
{
"orgName": "Shape-based Scratch Org",
"sourceOrg": "00D000000000000AAA"
}Then create the org with the file as usual:
sf org create scratch --definition-file config/shape-scratch-def.json --alias shape-scratch --duration-days 7 --target-dev-hub my-hubReplace the sample ID with your own source org ID. Do not put edition in a file that uses sourceOrg. The shape already decides the edition, and the two settings can conflict.
Clean up shapes
A shape is not a scratch org, so it does not count against your active scratch org limit. Delete shapes you no longer need, especially after a major release changes the source org's configuration. A stale shape builds scratch orgs that no longer match the org they model.
sf org delete shape --target-org my-prod --no-promptScratch org snapshots
Why use a snapshot
Creating a scratch org can take a few minutes. Deploying a large project into it, installing managed packages, and loading test data can take much longer. If you do that work for every new scratch org, a snapshot lets you do it once. You create the org, prepare it, and take a snapshot. Every later scratch org starts from that prepared state.
A snapshot fits these cases:
- The project depends on several installed packages that take a long time to install.
- A team shares a base scratch org with a standard set of test data.
- CI jobs spend most of their time on setup before the tests start.
Create a snapshot
First, prepare a scratch org the way you want every future org to look. Then snapshot it. The snapshot is stored in the Dev Hub, so the Dev Hub is the target org here.
sf org create snapshot --source-org my-scratch --snapshot-name BaseSnap --description "Packages and sample data" --target-org my-hub
sf org get snapshot --snapshot BaseSnap --target-org my-hub
sf org list snapshot --target-org my-hubSnapshot creation is also asynchronous. The status moves through New and InProgress to Active. If it ends in Error, the status message in sf org get snapshot explains why. Only an Active snapshot can be used.
Create a scratch org from a snapshot
Use the --snapshot flag with the snapshot name:
sf org create scratch --snapshot BaseSnap --alias from-snap --duration-days 7 --target-dev-hub my-hubThe new org is a copy of the snapshot, not a link to it. Changes you make in the new scratch org do not alter the snapshot. The new org still has its own expiry date and counts toward the Dev Hub's active and daily scratch org limits.
Keep snapshots current
A snapshot is frozen. If your project's source changes, orgs created from an old snapshot start out behind it. You have two choices:
- Deploy the latest source after creating the org. This is quick when the snapshot already holds the slow parts, such as packages and data.
- Create a new snapshot on a schedule or after major changes, then delete the old one.
Delete unused snapshots, because they consume Dev Hub resources and invite confusion about which one is current:
sf org delete snapshot --snapshot BaseSnap --target-org my-hub --no-promptChoosing between them
The diagram shows how the choice follows from the starting point.
You can combine them. Create a scratch org from a shape, deploy and prepare it, then snapshot it. New orgs then carry both the production-like configuration and your prepared state.
Common errors
- The shape is not listed or is not active. Shape creation is still running, or Org Shape is not enabled in the source org. Check the setting in Setup and run
sf org list shapeagain. - The scratch org fails to create from
sourceOrg. The ID may belong to a different org than the one you shaped, or the shape was deleted. Confirm the ID withsf org display. - The snapshot ends in
Error. Read the message fromsf org get snapshot. A source org that was changing or expired during the snapshot is a typical cause. - The snapshot name is not found. Snapshot names belong to one Dev Hub. Pass the same Dev Hub with
--target-dev-hub. - Limit errors. Orgs built from shapes or snapshots count toward the same active and daily limits you checked with
sf limits api display.
Exercise
A team's production org has several enabled features and a high storage limit. Developers keep reporting that deployments work in their scratch orgs but fail in a sandbox that matches production. Which tool should the team add, and which setting or flag connects it to scratch org creation?
Show answer
Use org shape. Enable Org Shape in the production org, run sf org create shape --target-org <prod-alias>, and wait for the shape to become active. Then create scratch orgs with --source-org <production org ID> or put "sourceOrg": "<production org ID>" in the definition file. A snapshot would not help here, because the problem is the org configuration, not repeated setup work.
Exercise
You created a snapshot named BaseSnap, but sf org create scratch --snapshot BaseSnap fails right away. What is the first thing to check, and which command shows it?
Show answer
Check the snapshot status with sf org get snapshot --snapshot BaseSnap --target-org my-hub. Only a snapshot with the status Active can be used. If it is still New or InProgress, wait. If it is Error, read the message and create the snapshot again. Also confirm that you are using the same Dev Hub that holds the snapshot.