Sub topic 14
Generating synthetic data instead of copying it
This chapter assumes you can export and import records with sf data commands, read an Apex anonymous script, and work with CSV and JSON files.
Why generate data
Exported data is realistic, but it carries risk. Production records can contain personal information, and the source org's data drifts over time. A dataset exported last month may not match today's schema.
Synthetic data is created from rules you write. It contains no real people, so it is safe in any scratch org or sandbox. You can also regenerate it on demand at any size.
| Concern | Copied data | Synthetic data |
|---|---|---|
| Contains real personal data | Possible | No |
| Reflects odd production edge cases | Yes | Only if you write them in |
| Volume | Limited by what you export | Set by a count |
| Reproducible | Only from a saved export | Yes, from the recipe |
Choosing between them
Option 1: a Snowfakery recipe
Snowfakery is an open source tool from Salesforce.org that builds records from a YAML recipe. This recipe creates 10 Accounts, each with 3 Contacts.
- object: Account
count: 10
fields:
Name:
fake: Company
Industry: Technology
friends:
- object: Contact
count: 3
fields:
FirstName:
fake: FirstName
LastName:
fake: LastName
Email:
fake: Email
AccountId:
reference: AccountThe friends block creates the child records. reference: Account links each Contact to the Account that generated it. Generate the output with:
pip install snowfakery
snowfakery recipe.yml --output-format jsonSnowfakery numbers its records itself, so its output does not contain real Salesforce IDs. Loading it into an org normally goes through CumulusCI, which maps those numbers to new IDs as it inserts. Check the Snowfakery documentation for the loading options your version supports.
Option 2: generate inside Apex
If you do not want another tool, build the values in the anonymous script from the earlier chapter. Deriving each value from the loop index keeps the output the same on every run.
List<Account> accounts = new List<Account>();
for (Integer i = 1; i <= 50; i++) {
accounts.add(new Account(
Name = 'Synthetic Account ' + i,
Industry = 'Technology',
NumberOfEmployees = i * 10
));
}
insert accounts;Anonymous Apex runs under governor limits, so keep a single run to a few thousand records. Use bulk loading for more.
Keeping generated data useful
- Add a recognizable prefix such as
Syntheticto names, so you can find and delete the records later. - Include deliberate edge cases: a blank optional field, a very long name, a record in each picklist value your code branches on.
- Satisfy validation rules and required fields in the recipe. Generated data fails an import the same way copied data does.
- Store the recipe in version control next to the project. The recipe is then your dataset.
Exercise
Your team's sandbox must not contain customer emails, but testers need 5,000 Contacts to check list performance. Which approach fits better, and why?
Show answer
Generate synthetic Contacts, for example with a Snowfakery recipe or a bulk CSV built from a script. No customer data is involved, and the count is a setting you can change. A masked export would work only if you also trusted the masking to be complete.