Sub topic 1
Why seed test data, and what good seed data looks like
Prerequisites
This chapter assumes you know the following:
- The Salesforce data model: objects, fields, and lookup and master-detail relationships.
- A Salesforce DX project layout, and how to run
sfcommands against an authorized org. - Basic JSON and CSV handling, and basic Apex syntax.
You don't need to run any commands yet. This chapter sets the goals that the later chapters build on.
Why seed test data
A new scratch org contains your metadata and almost no records. Tests, demos, and manual checks need records to work with, and typing them in by hand is slow and hard to repeat. Seeding means loading a known set of records into an org by script, so any developer or CI job can get the same starting state.
Good seed data helps in several ways:
- Developers can start work in a fresh org in minutes instead of building records by hand.
- Automated and manual tests start from the same state, so failures are easier to reproduce.
- Demos and reviews show realistic screens, not empty lists.
- Teams don't have to copy production data into environments where it doesn't belong.
What good seed data looks like
Good seed data has five qualities.
| Quality | What it means | What goes wrong without it |
|---|---|---|
| Small | Tens or hundreds of records, not millions | Slow setup and slow tests |
| Relational | Parents and children are linked, such as Accounts and their Contacts | Screens, reports, and triggers behave unrealistically |
| Repeatable | Running the load twice gives the same result | Duplicate records pile up |
| Safe | No real customer data in non-production orgs | Privacy and compliance risk |
| Versioned | Data files live in source control next to the metadata | Data and metadata drift apart |
Seed data should also cover the cases your code handles. Include a record that passes a validation rule and one that sits at its limit. Include an Account with no Contacts, and one with many.
The shape of a seeding workflow
Every approach in this tutorial follows the same order. Metadata comes first, because records can't exist without the objects and fields that hold them. Parent records come before child records, because a child needs its parent's ID.
The later chapters cover the tools for each step: CLI tree export and import, bulk upsert, Apex scripts, and sandbox post-copy classes.
Choosing an approach
The right tool depends on the environment, the volume, and how sensitive the data is.
- Scratch orgs start empty, so you load everything yourself. Small relational datasets suit tree import or Apex scripts.
- Sandboxes can start with copied data, depending on their type. You often add or adjust records after a refresh.
- Larger or repeatable loads suit bulk upsert with external IDs.
- Data that mirrors production needs masking or replacement. Synthetic data avoids the problem.
Common mistakes
- Seeding before the metadata is deployed. The load fails because objects or fields don't exist.
- Hard-coding record IDs. IDs differ in every org, so use references or external IDs instead.
- Copying production data into a scratch org without checking it for personal information.
- Writing a script that works only once, and leaves duplicates on the second run.
Exercise
A teammate's seed script inserts 200 Contacts with a hard-coded AccountId copied from their sandbox. It fails in a new scratch org. Give two reasons it fails and one way to fix it.
Show answer
Record IDs are unique to each org, so the copied AccountId doesn't exist in the scratch org. The script may also run before the Account is loaded, or before the metadata is deployed. Fix it by loading the Account first and linking Contacts through a reference ID, an external ID, or the Id returned by the insert.