Sub topic 3
Automating Setup After a Sandbox Copy
Assume you know Salesforce metadata, basic Apex, Git, and Salesforce CLI. A sandbox copy or refresh replaces its data and metadata with a snapshot from the source org. Any manual setup must be repeated unless you automate it.
Automating setup after a sandbox copy
A class that implements SandboxPostCopy runs after a sandbox is created or refreshed. Use it for repeatable, non-secret setup such as creating test records, changing configuration, or recording the refresh details.
global class PrepareSandbox implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
System.debug(
'Sandbox ' + context.sandboxName() +
' copied from org ' + context.organizationId()
);
// Add safe post-copy setup here.
}
}Keep the class idempotent. An idempotent operation produces the same result when it runs more than once. Avoid creating duplicate records or changing settings that developers must control manually.
Test data and protecting sensitive data
Choose the smallest data set that supports the test. Developer and Developer Pro sandboxes usually contain configuration but little or no production data. Partial Copy and Full sandboxes can contain copied production data, so protect personal and confidential information.
- Mask or replace sensitive fields before testing.
- Use synthetic records when production values are unnecessary.
- Restrict sandbox access to the people who need it.
- Never copy production credentials, tokens, or secrets into a sandbox.
- Let post-copy automation seed predictable test records.
Building an environment pipeline
A simple pipeline moves changes through increasingly realistic environments:
flowchart LR
A[Git branch] --> B[Scratch org]
B --> C[Integration sandbox]
C --> D[User acceptance sandbox]
D --> E[Production]caption: A source-driven Salesforce environment pipeline alt: Changes move from a Git branch to a scratch org, then through integration and acceptance sandboxes before production.
Use scratch orgs for short-lived feature work and automated checks. Use sandboxes for shared integration, user acceptance, and data-dependent testing.
Using scratch orgs in continuous integration
Continuous integration (CI) automatically validates each change. A CI job can create a scratch org from a definition file, deploy source, run tests, and delete the org. This gives each change an isolated environment and reduces configuration drift.
Scratch orgs are temporary and have allocation limits. Do not depend on them for long-running test data or shared manual testing.
Pitfalls and good practices
| Risk | Good practice |
|---|---|
| Stale sandbox | Refresh on a documented schedule |
| Unmasked data | Mask or synthesize sensitive values |
| Configuration drift | Store definitions and setup in version control |
| Duplicate post-copy records | Make automation idempotent |
Exercise
Which environment is usually better for an isolated CI validation: a shared sandbox or a scratch org?
Show answer
A scratch org, because CI can create and delete an isolated environment for each validation.