SfSkills Domains Docs GitHub

Home / devops / sandbox-refresh-and-templates

sandbox-refresh-and-templates

Sandbox refresh cycles, sandbox templates, post-refresh automation via the SandboxPostCopy Apex interface, and data handling during refresh. NOT for sandbox type selection — use admin/sandbox-strategy.

Open the skill folder on GitHub


Sandbox Refresh and Templates

This skill activates when a practitioner needs to run, plan, or troubleshoot a sandbox refresh — configuring sandbox templates, automating post-refresh setup with the SandboxPostCopy Apex interface, and handling data and integrations safely afterwards. It does not cover choosing which sandbox type to use (see admin/sandbox-strategy).


Before Starting


Questions to Ask Before Configuring

AskWhy it mattersWhat a good answer adds
Which scheduled jobs must run in this sandbox after refresh?Scheduled jobs from the source org aren't copied on refresh; they must be rescheduled (gotcha 2).A reschedule list the post-copy class or runbook executes.
What must the post-copy class do, and does the Automated Process user have access to those objects?The class runs as an Automated Process user that can't access all objects and features (gotcha 4).A scoped task list, a test that runs as that user, and a manual fallback.
Is this a Partial Copy sandbox, and which template does it use?templateId is required for Partial sandboxes, optional for Full, and not available for Developer or Developer Pro (gotcha 5).A named template with the objects the tests need.
Does any data copied into the sandbox contain personal data?Partial Copy and Full sandboxes receive production data (gotcha 6).A masking step in the post-copy class (or a dedicated masking product) before users get access.
Which external endpoints and credentials does the sandbox call?Copied configuration points at production systems until it is changed (gotcha 3).Endpoint and credential changes owned by the post-copy class or a runbook line.
Where are org IDs or production URLs stored as values?A sandbox is its own org; values copied from production are wrong there (gotcha 1).Code that reads the org ID at runtime instead of storing it.
Who refreshes, how, and when is the next window?Options such as the template are chosen per refresh, and a CLI refresh can change them through a definition file; refresh intervals limit how often you can retry (gotcha 7).A refresh owner, a definition file in source control, and a calendar.

A proper refresh setup adds a tested post-copy class, a template that matches the tests, and a short manual runbook, where "just clicking Refresh" produces a sandbox with no scheduled jobs, production endpoints, and unmasked customer data.


Core Concepts

What a refresh copies

Sandbox typeCopied from sourceTemplate
DeveloperMetadataNot available
Developer ProMetadataNot available
Partial CopyMetadata + data for the objects the template selectsRequired (templateId)
FullMetadata + dataOptional (templateId)

Refreshing updates a sandbox's metadata from the source org; if the sandbox is a clone or uses a template, the refresh also updates its data. A sandbox template lets you select which objects to copy. Scheduled jobs are not copied.

Refresh intervals: Developer and Developer Pro 1 day, Partial Copy 5 days, Full 29 days. UNVERIFIED (2026-10-19): these intervals are published in Salesforce Help ("Sandbox Licenses and Storage Limits by Type"), which does not fetch; confirm in Setup > Sandboxes before planning a calendar around them.

The SandboxPostCopy interface

System.SandboxPostCopy has one method, runApexClass(System.SandboxContext context); the context gives context.organizationId(), context.sandboxId(), and context.sandboxName(). You specify the class when creating or refreshing the sandbox (Setup's Apex Class field, or apexClassName / apexClassId in a CLI sandbox definition file). The class needs a no-arg constructor, because that is the constructor the copy process uses. It runs at the end of the copy as a special Automated Process user that isn't visible in the org and doesn't have access to all objects and features; if the script fails, run it after activation as a user with the right permissions.

global class SandboxSetup implements SandboxPostCopy {
    global SandboxSetup() {}   // the copy process uses the no-arg constructor

    global void runApexClass(SandboxContext context) {
        Sandbox_Config__c cfg = Sandbox_Config__c.getOrgDefaults();
        cfg.Environment_Name__c = context.sandboxName();
        cfg.Endpoint_Base_URL__c = 'https://sandbox-api.example.com';
        upsert cfg;

        // Scheduled jobs are not copied on refresh: schedule the ones this sandbox needs.
        System.schedule('Nightly sync (sandbox)', '0 0 2 * * ?', new NightlySyncSchedulable());
    }
}

Test it with Test.testSandboxPostCopyScript(script, orgId, sandboxId, sandboxName, true) — the overload whose last argument runs the test with the Automated Process user's permissions, which Salesforce recommends over the four-argument version.

Refreshing from the CLI

{
  "sandboxName": "uat",
  "licenseType": "Partial",
  "templateId": "1psXXXXXXXXXXXX",
  "apexClassName": "SandboxSetup",
  "autoActivate": true
}

sf org refresh sandbox --name uat --target-org prodOrg --definition-file config/uat-sandbox-def.json refreshes with that configuration; --name is the sandbox name, not its alias, and the name itself can't change on refresh. sandboxName is alphanumeric, 10 characters or fewer.


  1. Answer the Questions table; record the refresh owner, sandbox type, template, and post-copy class in templates/sandbox-refresh-and-templates-template.md.
  2. Write or update the SandboxPostCopy class in production with a no-arg constructor, job scheduling, endpoint changes, and masking; cover it with a Test.testSandboxPostCopyScript(..., true) test.
  3. Keep the sandbox definition file in config/ and run python3 skills/devops/sandbox-refresh-and-templates/scripts/check_sandbox_refresh.py --manifest-dir . — it ERRORs on a Partial definition without templateId, a template on Developer types, a post-copy class with no no-arg constructor, and conflicting definition options.
  4. Refresh (Setup or sf org refresh sandbox --definition-file ...), then confirm the post-copy work landed: config values, scheduled jobs in CronTrigger, masked fields.
  5. Run the manual runbook for what the class could not do (credentials, external system callbacks, email deliverability), and re-run the class as an admin if it failed.

Decision Guidance

SituationApproachReason
Repeatable setup after every refreshSandboxPostCopy class named on the sandboxRuns automatically at the end of the copy
Control which objects land in Partial CopySandbox template (templateId)Required for Partial sandboxes; selects objects
Need specific records, not a sampleDeveloper sandbox + scripted data loadA template selects objects, not individual records
Jobs needed in the sandboxSystem.schedule in the post-copy class or a runbook stepScheduled jobs aren't copied on refresh
Credentials for sandbox endpointsSandbox-specific Named/External Credentials; ConnectApi.NamedCredentials.createCredential where the running user has accessProduction credentials should not be reused
Org ID needed by codeUserInfo.getOrganizationId() at runtimeStored IDs copied from production are wrong in the sandbox

Review Checklist


Salesforce-Specific Gotchas

Full list in references/gotchas.md. The two that surprise teams most:

  1. Scheduled jobs are not copied — the sandbox starts with none of production's scheduled Apex.
  2. The post-copy user is limited — the Automated Process user can't reach every object; plan a manual re-run.

Output Artifacts

ArtifactDescription
Refresh readiness checklistInterval, template, post-copy class, owner
SandboxPostCopy class + testScheduling, endpoints, masking; tested as the Automated Process user
Sandbox definition fileconfig/<name>-sandbox-def.json with templateId and apexClassName
Manual post-refresh runbookCredentials, external callbacks, email deliverability

Reference Files

FileUse it for
references/examples.mdPost-copy class, Partial Copy template design, PII masking
references/gotchas.mdSeven refresh behaviours with avoidance steps
references/llm-anti-patterns.mdRefresh advice AI assistants get wrong
references/well-architected.mdPillar framing and the guide lines behind each claim
references/cli-refresh-runbook.mdCLI create/refresh with definition files and a post-refresh verification query set

Package files