Sub topic 11
Preparing a refreshed sandbox with a post-copy Apex class
This chapter assumes you can read basic Apex, know what a sandbox refresh does, and have a Salesforce DX project that you can deploy to a production org and to its sandboxes. Earlier chapters covered Apex anonymous scripts and sandbox templates. Here you automate the step that runs after Salesforce finishes copying a sandbox.
Why a post-copy class
A refreshed sandbox starts as a copy of production metadata, plus whatever data its type and template include. It rarely contains the exact records your team needs for testing. Without automation, someone has to run a seed script by hand after every refresh, and people forget.
A class that implements the SandboxPostCopy interface runs automatically after the sandbox is created or refreshed. You choose the class when you create or refresh the sandbox. Salesforce runs it as part of the copy process.
The class
The interface has one method, runApexClass. It receives a SandboxContext that tells you which sandbox is running.
global class SeedAfterRefresh implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug('Seeding sandbox ' + context.sandboxName());
insert new Account(Name = 'Post-copy Seed Account');
}
}The class and its method must be global. SandboxContext also exposes organizationId() and sandboxId(), which are useful when behavior must differ between sandboxes.
Deploy the class to production
The class must exist in the source org, normally production, before you refresh. A refresh copies the production metadata, so a class that exists only in another sandbox is not found. Deploy it with your usual process, then select it when you create or refresh the sandbox.
- Add
SeedAfterRefresh.clsto your project and deploy it to production. - In Setup, open Sandboxes and create or refresh the sandbox.
- In the Apex Class field, pick
SeedAfterRefresh. - Start the copy and wait for the completion email.
Common errors
- The class does not appear in the picker. It is not deployed to production, or it does not implement
SandboxPostCopy. - No records appear. Check the Apex Jobs page and the debug logs. A failure inside the class does not stop the sandbox from being created, so the sandbox looks fine even though seeding did not happen.
- A validation rule or trigger blocks the insert. The class runs with your production org's automation active. Seed records must satisfy the same required fields and rules as any other insert.
- Duplicate records after a second refresh. Refreshes start from a new copy, so duplicates are rare there. They can happen if your template already brings in the same records. Use an external ID and
upsertin the class if the data might already exist.
Keep it small
Post-copy classes run under normal Apex governor limits. Insert a small, known dataset, such as a few Accounts and Contacts, and use bulk lists rather than one DML statement per record. For larger datasets, load files with the CLI after the sandbox is ready, as covered in the bulk loading chapter.
Exercise
You refresh a sandbox and the SeedAfterRefresh class is selected, but no seed Account exists afterward, and the sandbox was created normally. Where do you look first, and why?
Show answer
Look at the Apex Jobs page and the debug logs in the sandbox. An exception inside runApexClass does not prevent the sandbox from being created, so the creation succeeding does not show that seeding worked. The logs show whether the class ran and what error it raised, such as a failed validation rule or a missing required field.