Sub topic 3
Designing a small dataset that respects object dependencies
This chapter assumes you know the Salesforce data model (objects, fields, lookup and master-detail relationships), have a Salesforce DX project, and can read JSON and CSV. It covers how to decide what goes into a seed dataset and in what order it must load. The later chapters cover the commands that load it.
Start from the tests, not from production
A seed dataset is the smallest set of records that lets a developer, a test, or a demo do its job. Production holds millions of rows, and most of them add nothing to that job. Start by listing the scenarios the data must support, then work backward to the records.
Ask these questions for each scenario:
- Which objects does the scenario touch?
- Which states must exist? For example, an open Opportunity, a closed-won Opportunity, and an Account with no Contacts.
- Which edge cases have caused bugs before? For example, a Contact with no email, or an Account with a very long name.
A dataset of 10 to 50 well-chosen records usually beats 5,000 random ones. It loads in seconds, a person can read it, and a failure points to a specific record.
Map the dependencies
Many objects hold a reference to another object. A record cannot be saved until the record it points to exists. That fixes the load order: parents first, then children.
There are two kinds of reference to plan for:
- A lookup field points to another record and is usually optional. If it is blank, the child still saves.
- A master-detail field is required. The child cannot exist without the parent, and the platform deletes the child when the parent is deleted.
A required lookup (made required by a field setting or by a validation rule) behaves like master-detail for loading purposes. Check the field definitions in your project, not only the standard object model.
Here is a typical sales dataset.
From the diagram you can read a valid order: Account, Product2, PricebookEntry, Contact, Opportunity, OpportunityContactRole, OpportunityLineItem. Several orders are valid. What matters is that no object appears before one it depends on.
Find the dependencies in your own org
Do not rely on memory for custom objects. You can read the relationship fields from the org itself. This command describes an object and prints the result as JSON:
sf sobject describe --sobject Opportunity --target-org my-source-org --jsonIn the output, look at each entry in fields where referenceTo is not empty. Fields with nillable set to false and createable set to true are required references. Those are the edges you cannot skip.
Watch for cycles and special cases
Most dependencies form a tree, but some do not. Handle these cases deliberately.
| Situation | Why it is a problem | Common fix |
|---|---|---|
| Self-lookup, such as Account `ParentId` | A record can point to another record of the same object | Load in two passes, or order records so parents come first within one file |
| Circular lookups between two objects | Neither can be saved first | Load both with the lookup blank, then update the lookup |
| Standard Pricebook entries | `PricebookEntry` for a custom pricebook needs a standard entry first | Load the standard price before any custom one |
| Record types | The record type ID differs in each org | Reference the type by developer name, not by ID |
| Owner fields | The source user may not exist in the target org | Drop `OwnerId` and let the loading user own the records |
| Users and profiles | Loaded users need valid profiles and unique usernames | Seed users with a script, not with the data files |
The record type and owner rows deserve emphasis. Salesforce IDs are specific to an org. An ID copied from one org will fail, or will point to the wrong record, in another. Keep IDs out of the dataset. Use relationships that the loading tool can resolve, which the next chapters show.
Choose which fields to keep
A record needs fewer fields than you think. Keep:
- Every required field, including required custom fields.
- Fields that scenarios depend on, such as
StageName,Industry, orCloseDate. - A stable business key, if you plan to reload the data. Chapter material on external IDs explains why.
Remove:
- System fields such as
CreatedDateandLastModifiedById, which the load sets itself. - Formula, roll-up summary, and auto-number fields, which cannot be written.
- Fields that point to records outside the dataset.
If a field is read-only in the target org, the load fails for that record. Field-level security for the loading user can also hide a field and cause the same failure.
Keep the data realistic enough, and safe
Fake values should look like real ones where it matters. Validation rules, duplicate rules, and flows run during a load, so a Contact with the email x may be rejected. Use values such as [email protected]. The domain example.com is reserved for documentation and never delivers mail, so it is safe if an email-sending flow fires.
Do not copy real customer names, emails, or phone numbers into seed files. Files in your repository are visible to everyone with repository access and live in version history permanently. A later chapter covers masking. For now, design records so they never need it: write them by hand or generate them.
A worked design
Suppose the team is building a feature that shows an Account's open Opportunities and its primary Contact. The scenarios are:
- An Account with two open Opportunities and a primary Contact.
- An Account with only a closed Opportunity.
- An Account with no Opportunities and no Contacts.
That needs three Accounts, two Contacts at most, three Opportunities, and one OpportunityContactRole. The design can be written as a table before any file exists.
| Order | Object | Records | Depends on |
|---|---|---|---|
| 1 | Account | 3 | none |
| 2 | Contact | 2 | Account |
| 3 | Opportunity | 3 | Account |
| 4 | OpportunityContactRole | 1 | Contact, Opportunity |
Eight or nine records are enough to cover every scenario. Each row of the table later becomes one entry in a plan file, so the design work is not wasted.
Give every record a name you can trace
Use readable, predictable values: Seed Account Open Deals, Seed Account Closed Only, Seed Account Empty. A prefix such as Seed lets you find and delete the records later with a query like WHERE Name LIKE 'Seed%'. It also makes it obvious in the UI that the data is not real.
Common mistakes
- Designing from the largest object down. Start from scenarios, then add only the parents those records need.
- Ignoring automation. Triggers, flows, and validation rules fire during loads. A flow that creates a Task for every new Opportunity will add records you did not plan.
- Hard-coding IDs. They break the first time the data moves to another org.
- Forgetting limits. A scratch org has storage limits, and a Developer sandbox has a small data allowance. Small datasets avoid the problem.
Exercise
A seed dataset needs Account, Contact, Opportunity, and a custom object Invoice_c. Invoicec has a required lookup to Opportunity and an optional lookup to Contact. Give a valid load order, and explain what would happen if Invoice_c were loaded before Opportunity.
Show answer
A valid order is Account, Contact, Opportunity, Invoice_c (Contact and Opportunity can swap places, since both depend only on Account). Invoicec must come last because its required lookup to Opportunity needs an existing Opportunity record. If Invoicec were loaded first, each Invoice_c record would fail to save, because the required Opportunity reference could not be resolved to an existing record.
Exercise
Why should a seed file avoid raw record type IDs and OwnerId values copied from a source org?
Show answer
Salesforce IDs belong to a single org. An ID from the source org does not exist in the target org, so the load fails or points at the wrong record. Use the record type developer name, and let the loading user own the records.