Sub topic 3
Designing a small dataset that respects object dependencies
This chapter assumes you know Salesforce objects, fields, and lookup and master-detail relationships, and that you have a Salesforce DX project. Before you export or import anything, decide what the dataset contains. A small, deliberate dataset is easier to load, easier to reason about, and faster to reset than a large copy of production.
Start from the tests, not from the org
List the scenarios the data must support. For example, "an Account with several Contacts" or "an Opportunity in each stage". Then work backward to the smallest set of records that covers them. Ten Accounts with a few Contacts each is usually enough to test a trigger, a flow, or a Lightning page.
Good seed data has these properties:
- Small. Fewer records load faster and fail less often.
- Representative. It includes the edge cases your code handles, such as a Contact with no email.
- Connected. Child records point at real parents.
- Safe. It contains no real customer data (covered in a later chapter).
Map the dependencies
A child record cannot be inserted until the record it references exists. A required master-detail field must be filled at insert time. A lookup can be empty unless the field is marked required or a validation rule demands it.
Draw the dependency order before you write any file. Parents come first.
Read the arrows as "must exist before". Account has no dependencies, so it loads first. OpportunityContactRole references both a Contact and an Opportunity, so it loads last.
Record the order in a table
Write the load order down with the fields that create each dependency. This table becomes the skeleton of your plan file.
| Order | Object | Depends on | Field that links it |
|---|---|---|---|
| 1 | Account | none | none |
| 2 | Contact | Account | AccountId |
| 2 | Opportunity | Account | AccountId |
| 3 | OpportunityContactRole | Contact, Opportunity | ContactId, OpportunityId |
Objects with the same order number do not depend on each other. You can load them in either order, but both must come after Account.
Watch for hidden dependencies
The visible relationships are only part of the picture. Check for these as well:
- Required fields. Any field marked required on the object or its page layout rules must have a value.
- Validation rules. A rule that requires a phone number will reject a record that omits one.
- Record types and picklists. Values must exist in the target org. Metadata must be deployed before data.
- Users. Fields such as OwnerId point at Users, and a scratch org has different Users from your source org. Leave the owner unset so it defaults to the running user.
- Automation. Triggers and flows fire on insert and can create extra records or fail.
Keep it deterministic
Give each record a stable identity. In the files you will build later, each record has a reference ID such as AccountRef1, and a later chapter adds external IDs for upserts. Choose names that you can recognize in the org, for example "Seed Account 01". This makes it simple to find, check, and delete seed records.
Exercise
You need Contacts and Opportunities that both belong to Accounts, plus OpportunityContactRole records that join them. In which order must the objects load, and why can Contact and Opportunity load in either order?
Show answer
Load Account first, then Contact and Opportunity, then OpportunityContactRole. Contact and Opportunity each depend only on Account, not on each other, so either can go second. OpportunityContactRole references both, so it must come last.