Sub topic 13
Pitfalls and good practices
This chapter assumes you have read the earlier chapters on sandbox types, scratch orgs, and the environment pipeline. You should also be comfortable with the Salesforce CLI and Git. It collects the mistakes teams most often make with these org types and the habits that prevent them.
Stale sandboxes
A sandbox is a copy of production taken at one moment. Over time it falls behind. Production gets new metadata, new data patterns, and new integrations, and a sandbox that has not been refreshed no longer matches them. Tests that pass there can then fail after deployment.
Refresh intervals limit how often you can fix this. A Developer or Developer Pro sandbox can be refreshed once a day, a Partial Copy sandbox every 5 days, and a Full sandbox every 29 days. Plan around these intervals. Do not discover on release day that your Full sandbox cannot be refreshed again for three weeks.
Good practices:
- Refresh the sandbox that does final validation shortly before each major release.
- Before refreshing, commit or deploy any work that exists only in the sandbox. A refresh replaces its contents.
- Put repeatable setup in a
SandboxPostCopyclass so a refresh does not depend on someone remembering manual steps.
Unprotected data
A Full or Partial Copy sandbox can contain real customer records. Developers and testers who would never get production access can read that data, and test emails or integrations may reach real people.
- Mask or scramble sensitive fields after each copy. Salesforce offers Data Mask for this. You can also do it in your post-copy class.
- Deactivate outbound integrations and change email deliverability settings in non-production orgs.
- Prefer synthetic seed data in scratch orgs and Developer sandboxes. These org types do not need real records.
Configuration drift
Drift is any difference between orgs that nobody planned. It usually starts with a change made directly in one org, such as an urgent fix in production or a field added by hand in a sandbox.
To reduce drift:
- Treat version control as the source of truth, not any single org.
- Make changes in a scratch org or Developer sandbox, then commit them.
- If production must be changed directly, retrieve that change into Git the same day.
- Keep the scratch org definition file in the repository, so every new org starts from the same shape.
Scratch org mistakes
- Treating scratch orgs as long-lived. They expire, with a maximum lifespan of 30 days. Anything not in source control is lost at expiry.
- Forgetting the limits. A Dev Hub caps how many scratch orgs can be active and how many can be created per day. Check your remaining allocation before a busy period.
- A definition file that does not match production. If features or settings are missing from it, the scratch org behaves differently from the org you deploy to.
- Ignoring source tracking conflicts. Before you pull, review what changed. Do not overwrite local work blindly.
These two commands help with housekeeping:
sf org list limits --target-org devhub
sf org list --cleanThe first shows the Dev Hub's remaining scratch org allocation. The second removes expired orgs from your local list.
Matching the org to the job
Many problems come from using the wrong org type. Do not run realistic performance tests in a Developer sandbox, because it has little data. Do not do day-to-day feature work in a Full sandbox, because refreshes are slow and the org is shared. Use the decision framework from earlier in this tutorial, then confirm the choice against the refresh interval and data needs.
Exercise
A team's Full sandbox was last refreshed 10 weeks ago. Testers report that a release passed there but failed in production. Name two likely causes and one process change that would help.
Show answer
Likely causes are configuration drift (changes made in production, such as a hotfix, that never reached the sandbox) and outdated data or metadata in the sandbox. A useful process change is to schedule a refresh of the validation sandbox shortly before each release, within the 29-day Full sandbox interval. Also keep Git as the source of truth so production changes are retrieved and committed.