Sub topic 17
Planning refreshes: schedules and best practices
This chapter assumes you know the four sandbox types and their refresh intervals. It also assumes you know what a refresh replaces, and that you have seen the pre-refresh and post-refresh steps from earlier chapters. Here you will combine them into a repeatable plan that a team can follow without losing work.
Why refreshes need a plan
A refresh replaces a sandbox's metadata and data with a new copy of its source org. Anything in the sandbox that is not in version control or a deployment package is lost. Refreshing without planning causes three common problems:
- A developer's unfinished work disappears.
- A QA cycle is interrupted halfway through.
- Post-refresh settings, such as integration endpoints, are forgotten, and the sandbox talks to the wrong systems.
A plan answers four questions. When may each sandbox be refreshed? Who must be told? What is saved first? What runs afterward?
Match the schedule to the refresh interval
Each type has a minimum interval between refreshes. Your schedule cannot be shorter than that interval, and it should usually be longer.
| Sandbox type | Minimum interval | Typical cadence | Typical trigger |
|---|---|---|---|
| Developer | 1 day | Per feature or sprint | Developer starts new work |
| Developer Pro | 1 day | Per sprint | Start of a sprint or release branch |
| Partial Copy | 5 days | Every 2 to 4 weeks | Start of a QA or UAT cycle |
| Full | 29 days | Monthly or per release | Before performance tests or release rehearsal |
The interval is a limit, not a goal. A Full sandbox that is refreshed every 29 days will often be mid-test when the window opens. Pick refresh dates around the project calendar instead.
Build a refresh calendar
Keep one shared calendar with these entries:
- Release dates. Never refresh a UAT or Full sandbox in the days before a release rehearsal.
- Seasonal Salesforce releases. Salesforce upgrades sandboxes before production on a preview schedule. Know whether a sandbox is on the preview instance, because a refresh can move it to a different release than production.
- Refresh windows per sandbox. Name an owner for each one.
- Freeze periods. Note month-end or quarter-end, when finance and sales data matter most.
Refresh sandboxes in order of dependence
Sandboxes often form a chain. Developer sandboxes feed an integration sandbox, which feeds UAT. If you refresh UAT from production while developers are still working on older metadata, UAT stops matching what developers will deploy.
A safe order is:
- Refresh the upstream test sandbox, such as UAT or Full, from production.
- Deploy any change sets or packages that are approved but not yet in production.
- Refresh Developer sandboxes afterward, so they start from the same baseline.
If a sandbox is cloned from another sandbox, remember that it copies the source sandbox's current state, including its unfinished changes. Clone only from a clean, verified source.
Protect work before the refresh
Treat the refresh as a delete followed by a create. Before you request it:
- Confirm that all metadata changes are in version control or a deployment package.
- Export data you need, such as test records created by hand. Record them in a data load file or a data plan.
- Document sandbox-only settings, such as named credentials, remote site settings, and email addresses. Better still, script them.
- Tell users which day the sandbox will be replaced.
When you request the refresh, you can choose to activate the new copy later. Until you activate it, the old sandbox remains available. This gives users time to finish, but activation then deletes the old copy. Do not activate until the owner confirms that the work is saved.
Automate the post-refresh work
The manual checklist after a refresh is long. Usernames get a sandbox suffix, email deliverability is limited to system email, and endpoints still point at production values. Automate what you can with a SandboxPostCopy class. Select it when you create or refresh the sandbox. It runs once, after the copy is complete, as the user who started the process.
The class below does two things. It records which sandbox was prepared, and it starts a batch job that masks contact email addresses so that test messages cannot reach real customers.
global class PrepareSandbox implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug('Preparing sandbox ' + context.sandboxName()
+ ' (' + context.sandboxId() + ')');
Database.executeBatch(new MaskContactEmails(), 200);
}
}
global class MaskContactEmails implements Database.Batchable<SObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT Id, Email FROM Contact WHERE Email != null AND (NOT Email LIKE \'%.invalid\')'
);
}
global void execute(Database.BatchableContext bc, List<Contact> scope) {
for (Contact c : scope) {
c.Email = c.Email + '.invalid';
}
Database.update(scope, false);
}
global void finish(Database.BatchableContext bc) {
System.debug('Contact email masking finished');
}
}Two details matter. The query skips emails already ending in .invalid, so the job is safe to run again. The false argument lets the batch continue past a record that fails validation, instead of failing the whole chunk. Note that sandbox copies of contacts keep their original email values, unlike user emails, which Salesforce modifies.
Test the post-copy class
Salesforce lets you test a SandboxPostCopy class without a refresh. Write a test so a change to the class cannot break the next refresh silently.
@isTest
private class PrepareSandboxTest {
@isTest
static void masksContactEmails() {
insert new Contact(LastName = 'Test', Email = '[email protected]');
Test.startTest();
Test.testSandboxPostCopyScript(
new PrepareSandbox(),
UserInfo.getOrganizationId(),
UserInfo.getOrganizationId(),
'dev1'
);
Test.stopTest();
Contact c = [SELECT Email FROM Contact LIMIT 1];
System.assertEquals('[email protected]', c.Email);
}
}Verify after the refresh
Run a short checklist before reopening the sandbox. Keep it in the same place as the calendar.
- Log in with a test user. Remember that usernames now end with the sandbox name.
- Check email deliverability under Setup. After a refresh it is limited to system email. Raise it only if the sandbox needs to send test messages, and only after masking addresses.
- Confirm that integration endpoints, named credentials, and connected apps point at test systems.
- Check scheduled jobs and outbound messages. Confirm none fires against production systems.
- Run a smoke test of the main business process.
- Reload any test data you exported earlier.
Common errors
- Refreshing the wrong sandbox. Names such as
dev1anduat1look similar. Confirm the name on the request screen. - Treating the refresh as a backup. A sandbox is not a backup of production, and a refresh is not a rollback of the sandbox.
- Skipping the post-copy test. A failing
SandboxPostCopyclass can leave the sandbox half-prepared. Check the logs after each refresh. - Long refresh cycles on Developer sandboxes. Metadata drifts from production, and merge conflicts follow. Refresh them regularly.
- Forgetting dependent sandboxes. A refreshed upstream sandbox breaks the baseline for downstream ones.
Exercise
Your team has a Full sandbox used for UAT and several Developer sandboxes. A release rehearsal is in six days, and the Full sandbox was last refreshed 30 days ago. Should you refresh it now? State your reasoning and name two things to do before and after.
Show answer
Probably not right away. The Full sandbox is eligible, since the interval is 29 days. However, a refresh has to be activated and set up again, and that uses time you need for the rehearsal. A better choice is to refresh it as soon as the current UAT cycle ends, or after the rehearsal if the data is still valid. If you refresh it, do these two things. Before: save all unsaved metadata and test data to version control or a data file, and tell UAT testers. After: run the SandboxPostCopy class, then verify usernames, email deliverability, and integration endpoints before reopening the sandbox. Refresh the Developer sandboxes after UAT so they share the same baseline.