Sub topic 10
Test data and protecting sensitive data
This chapter assumes you can create sandboxes and scratch orgs, read a definition file, and run sf commands. It covers what data each org type holds, how to seed test data, and how to keep sensitive production data out of non-production orgs.
What data each org type starts with
The org type decides how much production data you get. That affects both test quality and risk.
| Org type | Starting data | Sensitive-data risk |
|---|---|---|
| Developer sandbox | Metadata only, no records | Low |
| Developer Pro sandbox | Metadata only, no records | Low |
| Partial Copy sandbox | Metadata plus a sample of records, selected by a sandbox template | Medium |
| Full sandbox | Metadata plus all records | High |
| Scratch org | Metadata you deploy, plus records only if you load them | Low |
A Full sandbox is a complete copy of production. Every customer email address, phone number, and note is in it, and more people can reach a sandbox than can reach production. Treat a Full sandbox as production-sensitive.
Seeding data in scratch orgs and small sandboxes
Scratch orgs and Developer sandboxes start empty, so you create the records yourself. Two choices are common.
- Set
hasSampleDatatotruein the scratch org definition file to get the standard sample records. The earlier example set it tofalse, so that org starts empty. - Load your own records from files kept in version control.
The CLI can import records from JSON files. The commands below export a few accounts and their contacts from a source org into a plan, then import them into a scratch org.
sf data export tree --query "SELECT Name, Industry, (SELECT LastName, Email FROM Contacts) FROM Account LIMIT 5" --plan --output-dir data --target-org dev1
sf data import tree --plan data/Account-Contact-plan.json --target-org my-scratchOnly export from an org whose data is already safe. Do not export from production, because the files would land in your repository with real customer values.
Keeping seed data safe in version control
Seed files are source code, so write them by hand or generate them. Use fake values that cannot belong to a real person, such as @example.com addresses.
{
"records": [
{
"attributes": { "type": "Account", "referenceId": "AccountRef1" },
"Name": "Acme Test Account",
"Industry": "Technology"
},
{
"attributes": { "type": "Contact", "referenceId": "ContactRef1" },
"LastName": "Tester",
"Email": "[email protected]",
"AccountId": "@AccountRef1"
}
]
}The @AccountRef1 value points to the account in the same file, so the import links the contact to the account without a real record ID.
Protecting data in sandboxes that copy production
For Partial Copy and Full sandboxes, production data arrives automatically. Reduce the exposure with these steps.
- Mask the data. Salesforce Data Mask can replace sensitive field values in a sandbox with scrambled or fake ones. Run it right after the copy, before developers log in.
- Limit the copy. Use a sandbox template with a Partial Copy sandbox so only the objects you need are copied.
- Restrict access. Give only the people who need the sandbox a user account there.
- Block outbound messages. Sandbox email deliverability defaults to System email only after a copy. Leave it that way until the data is masked.
The SandboxPostCopy class from the previous chapter is a good place to automate these steps, because it runs after every create and refresh. This way, a refresh cannot silently put unmasked data back.
<br>
Common mistakes
- Assuming a sandbox is private. Sandbox users, integrations, and connected apps often have more reach than expected. Check them after each refresh.
- Leaving email deliverability on. Test records copied from production can send real emails to real customers.
- Committing exports. A data export from a real org, checked into Git, stays in the history even after you delete the file.
- Masking only once. A refresh replaces the sandbox with fresh production data, so masking must run again each time.
Exercise
Your team refreshes a Full sandbox every month, and testers log in the same day. After the last refresh, a tester found real customer email addresses in the contacts. What two changes would you make to the process?
Show answer
Add data masking as an automated step that runs after every refresh, for example from the SandboxPostCopy class or a Data Mask job, and hold back user access until it finishes. Also keep email deliverability at System email only until masking completes. If testers do not need all records, consider switching to a Partial Copy sandbox with a template.