Sub topic 1
Why Seed Test Data, and What Good Seed Data Looks Like
Seed data is a small, known set of records that you load into a Salesforce org so that developers, testers, and automated tests have something to work with. This chapter explains why seeding matters, what separates good seed data from bad, and the principles the rest of the tutorial builds on. Later chapters cover the commands and tools.
Prerequisites
This chapter assumes you have the following:
- Basic Salesforce data model knowledge: objects, fields, and lookup and master-detail relationships.
- The Salesforce CLI (
sf) installed and authorized to a Dev Hub or sandbox. - Familiarity with a Salesforce DX project layout.
- Basic JSON and CSV handling.
- Basic Apex syntax.
You do not need to run any commands yet. Later chapters use the sf data commands and Apex scripts.
Why seed test data
A new scratch org contains your metadata after you deploy it, but it has no business records. A freshly created or refreshed sandbox is different, because its contents depend on the sandbox type and on any template you chose. In both cases, you cannot rely on the org having the records your work needs.
Without seeding, people fall back on habits that cause trouble:
- Each developer creates records by hand. The records differ from person to person, so bugs appear for one developer and not another.
- Testers spend the first part of every session creating setup data instead of testing.
- Teams copy production data into lower environments. This risks exposing personal or confidential information.
- Demos and manual checks depend on whatever data happens to be in a shared sandbox.
Seeding replaces these habits with one repeatable step. Anyone can create an org, run the seed step, and reach the same starting state.
What seeding is not
Seed data is not the same as the test data created inside Apex test classes. Apex tests should create their own records, because they run in isolation and should not depend on org data. Seed data serves people and external tools: manual testing, UI development, demos, integration tests, and end-to-end test suites.
Properties of good seed data
Good seed data has these properties. Use them as a checklist when you design a dataset.
| Property | Meaning | Why it matters |
|---|---|---|
| Small | Tens or hundreds of records, not millions | Fast to load, easy to understand, cheap to rebuild |
| Representative | Covers the record types, statuses, and relationships your features use | Features behave as they will in production |
| Deterministic | Same input produces the same result each time | Failures can be reproduced |
| Relationally valid | Child records point at real parents | Lookups, roll-ups, and reports work |
| Repeatable | Can be re-run without creating duplicates or errors | Safe for local use and CI |
| Safe | Contains no real personal or confidential data | Avoids compliance and security problems |
| Versioned | Stored in source control next to the metadata | Data changes are reviewed and tracked with the code |
Small and representative
Small is a goal because every record you add has a cost. It takes time to load, it can trigger automation, and a developer has to understand it. Start with the minimum dataset that exercises your features, then add records when a real scenario needs them.
Representative does not mean large. It means covering the variety that matters. For example, an Account dataset for a sales app might include:
- One Account with no Contacts, to test empty states.
- One Account with several Contacts, to test lists.
- One Account in each Industry your validation or sharing logic treats differently.
- One record that is deliberately awkward, such as a very long name or a missing optional field.
Deterministic and relationally valid
Deterministic data means you can predict what is in the org. If a test expects an Account named Seed Account, that record must always exist with that name. Random data is useful in some cases, but randomness makes failures hard to reproduce. If you use generated data, fix the random seed or pin the generator's input.
Relational validity is the property that most often breaks. Records in Salesforce depend on each other. A Contact needs an Account (if your org requires one), and an Opportunity needs an Account and often a Pricebook. You cannot insert a child before its parent exists, because the child needs the parent's ID. IDs are created by the org on insert, so they differ in every org. Good seed data therefore avoids hard-coded Salesforce IDs and uses a reference mechanism instead. The tutorial covers reference IDs and external IDs in later chapters.
Repeatable and safe
A repeatable seed process can run more than once. Running a plain insert twice creates duplicates. Upserts keyed on an external ID update existing records instead. This tutorial returns to that topic when it covers external IDs.
A safe dataset contains no real customer data. Even in a sandbox, production-derived data can include names, emails, and phone numbers that fall under privacy rules. Prefer invented values such as [email protected]. The domain example.com is reserved for documentation, so mail sent there never reaches a real person.
The seeding workflow at a glance
Most seeding approaches follow the same sequence, regardless of the tool.
Metadata comes first. Objects, fields, record types, and validation rules must exist before data can be loaded into them. Parents load before children. Verification closes the loop, and a failed check sends you back to correct the dataset.
Choosing an approach
Several tools can seed data, and each fits a different situation. The table gives a first orientation. Later chapters cover each one in detail.
| Approach | Best for | Main limitation |
|---|---|---|
| CLI tree export and import | Small related datasets, such as Accounts with Contacts | Limited record volume per call |
| Bulk upsert with external IDs | Larger or repeated loads | Needs an external ID field and CSV preparation |
| Apex anonymous script | Logic-driven data, and records that need code to build | Counts against Apex limits, and the data is defined in code |
| Sandbox template and post-copy class | Preparing sandboxes at creation or refresh | Only applies to sandboxes |
| Synthetic data generation | Realistic volume without real data | Needs an extra tool and recipe |
Three factors drive the choice: the environment type, the data volume, and the data sensitivity. A scratch org is disposable and starts empty, so a scripted load fits well. A sandbox may keep data across refreshes, so you must think about what is already there. Sensitive source data pushes you toward synthetic or masked data.
Common mistakes
These mistakes appear often in real projects.
- Hard-coding record IDs. IDs differ between orgs. A script that works in one org fails in the next.
- Seeding before metadata exists. The load fails because fields or objects are missing.
- Copying production data unmasked. This creates a privacy risk in an environment with weaker access controls.
- Loading too much. Big datasets slow every org creation and make CI jobs slow.
- Ignoring automation. Triggers, flows, and validation rules run during loads. They can change your data or reject it. Know what is active in the target org.
- One-off manual steps. If seeding needs a person to click through a UI, it is not repeatable.
A short example of a seed design
Before running any command, write down the dataset on paper. A simple design for a sales app looks like this:
- Five Accounts across two Industries.
- Two Contacts for each Account, so ten Contacts in total.
- One Account with no Contacts, as an empty case.
- All values invented, with emails on
example.com.
This design is small, covers the variety you need, and follows the dependency order of Account before Contact. The next chapters turn a design like this into files and commands.
Exercise
A teammate proposes loading a full copy of production Accounts and Contacts into every scratch org so that testing is "realistic." Using the properties of good seed data, give two reasons this is a poor plan and one alternative.
Show answer
Two reasons: the data is not small, so every org takes longer to build and CI slows down, and it is not safe, because real names and emails would sit in a non-production environment with weaker controls. It also would not be deterministic, since production changes over time. An alternative is a small hand-designed dataset, or synthetic data, that covers the same variety of record types, statuses, and relationships using invented values.
Exercise
Why can a seed script not simply hard-code the ID of an Account when it creates Contacts?
Show answer
Salesforce generates record IDs when records are inserted, so the same Account has a different ID in every org. A hard-coded ID would not match in a new scratch org. The script needs a reference mechanism, such as reference IDs in a plan file or an external ID field, to link children to their parents.