Sub topic 3
Preparing for a Salesforce Sandbox Refresh
Assume you know the difference between Salesforce metadata and data, and that you can use Setup and Apex. A refresh replaces the sandbox with a new copy of its source org. Treat it as a destructive operation for sandbox-only work.
Preparing for a refresh: protect your work
Before starting:
- Commit or deploy uncommitted metadata and Apex.
- Export sandbox-only records that you need to keep.
- Record users, permission assignments, test data, and integration settings.
- Tell active users when the sandbox will be unavailable.
- Disable jobs, flows, and integrations that could send data to external systems.
Running a refresh step by step
- Open Setup and search for Sandboxes.
- Find the sandbox and select Refresh.
- Confirm the source org and sandbox type.
- Select a sandbox template when the type supports one.
- Start the refresh and monitor its status.
- After completion, activate the refreshed sandbox if Salesforce provides that option.
The refresh can take time. Do not assume that a successful request means the sandbox is ready immediately.
What changes after a refresh
The sandbox receives metadata and data from its source, subject to its sandbox type and template. Sandbox usernames usually include the sandbox name, while user passwords are not copied. Email deliverability and integration endpoints need verification because settings may be restricted, copied, or reset.
Check these items before allowing normal use:
- Email deliverability and notification addresses
- Named credentials, connected apps, and external endpoints
- Scheduled jobs, flows, and Apex automation
- User access and test data
- Remote systems that might reject the sandbox identity
Automating post-refresh setup with `SandboxPostCopy`
A class implementing SandboxPostCopy runs after Salesforce creates or refreshes a sandbox.
global class PrepareSandbox implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug('Sandbox: ' + context.sandboxName());
Organization org = [SELECT IsSandbox FROM Organization LIMIT 1];
if (org.IsSandbox) {
System.debug('Apply sandbox-only setup');
}
}
}Keep this class safe to run repeatedly. Do not send real customer emails or change production records.
Creating a sandbox with the Salesforce CLI
A definition file can make sandbox creation repeatable.
{
"sandboxName": "dev1",
"licenseType": "Developer"
}sf org create sandbox --definition-file config/sandbox-def.json --alias dev1 --target-org prodPlanning refreshes: schedules and best practices
Choose a regular maintenance window, announce it, and keep a checklist for pre-refresh exports and post-refresh validation. Refresh when the sandbox needs current metadata or representative data, not merely because the calendar says so. Store the checklist and SandboxPostCopy class in source control.
Exercise
What should you do before refreshing a sandbox that contains unique test records?
Show answer
Export or otherwise preserve the records, because the refresh can replace sandbox data with data from the source org.