Sub topic 15
Automating post-refresh setup with SandboxPostCopy
This chapter assumes you know how to create and refresh a sandbox from Setup. It also assumes you can write and deploy a basic Apex class.
Why automate the cleanup
A refresh replaces the sandbox with a fresh copy of its source org. Settings you adjusted by hand, such as integration endpoints, are lost or replaced by production values. Copied production data can also include real customer email addresses. Repeating these fixes by hand after every refresh is slow and easy to get wrong.
SandboxPostCopy is an Apex interface. Salesforce runs your class automatically once the sandbox copy finishes, so the same setup runs after every create or refresh.
Writing the class
The interface has one method, runApexClass. It receives a SandboxContext that identifies the sandbox. The class and method must be global.
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());
List<Contact> contacts = [
SELECT Id, Email FROM Contact WHERE Email != null LIMIT 10000
];
for (Contact c : contacts) {
c.Email = c.Id + '@example.invalid';
}
Database.update(contacts, false);
}
}This version masks contact emails so the sandbox cannot send messages to real customers. Use the same pattern for other tasks, such as pointing settings at test endpoints or scheduling jobs. The sandboxName() value lets one class behave differently in a QA sandbox than in a development sandbox.
Testing the class
You can test the class without running a real refresh. Test.testSandboxPostCopyScript runs it inside a test method.
@IsTest
private class PrepareSandboxTest {
@IsTest
static void runsWithoutError() {
Id orgId = UserInfo.getOrganizationId();
Test.startTest();
Test.testSandboxPostCopyScript(new PrepareSandbox(), orgId, orgId, 'dev1');
Test.stopTest();
System.assert(true, 'Script completed');
}
}Add real assertions about the records your script changes.
Attaching it to a sandbox
- Deploy the class to production. A sandbox copies metadata from its source, so the class must exist there before the copy.
- Start a create, clone, or refresh from Setup.
- In the dialog, choose your class in the Apex class field.
Salesforce runs it once the sandbox is ready. You must select the class each time, so record the choice in your refresh checklist.
Common mistakes
- Marking the class
publicinstead ofglobal, so it does not appear in the selection list. - Forgetting to deploy a changed version to production before refreshing.
- Hard-coding values that differ between sandboxes instead of branching on
sandboxName().
Exercise
Your refreshed QA sandbox keeps calling the production endpoint of an external service. How would you fix this permanently, and where must the fix live before the next refresh?
Show answer
Write a SandboxPostCopy class that updates the endpoint setting to the test value, possibly based on context.sandboxName(). Deploy it to production so the sandbox gets it in the copy, then select it in the refresh dialog.