Salesforce master-detail test data fails for one reason more than any other: the detail record gets inserted before its master exists, or the master ID it points to is wrong by the time the batch runs. Master-detail relationships enforce a strict parent-before-child dependency, and Bulk API v2 does not understand that dependency on its own. You have to build it in, or your load job throws REQUIRED_FIELD_MISSING errors on the master-detail field and stalls out halfway through a multi-object chain.
This is a sequencing problem, not a schema problem. Most admins know the rule "insert parents first." Fewer have a repeatable way to enforce it across three or four levels of nested master-detail, especially when the schema changes every sprint and nobody updates the load script.
Why master-detail test data breaks on load order
A lookup relationship tolerates a null reference. A master-detail field does not. The moment you define a field as master-detail, Salesforce makes it required on insert, non-nullable after creation in most configurations, and tied to the parent for security and rollup behavior. Your test data generator has to respect all three constraints at once.
Say you have Invoice__c as master and Invoice_Line__c as detail. If your generator creates 500 line records before a single invoice exists, every one of those inserts fails immediately. Scale that to a CPQ-style object graph with four or five nested master-detail layers and a single ordering mistake can wipe out an entire batch job, not just the records tied to it.
The fix sounds obvious: insert in dependency order. The practical challenge is that most teams generate test data table by table, from a spreadsheet or a flat CSV, with no awareness of which object is a master-detail parent and which is a detail child. That gap is where load jobs go wrong.
The parent-before-child rule and what it actually requires
Parent-before-child sounds simple until you account for how Bulk API v2 batches work. A single CSV upload does not guarantee row-by-row sequential processing. Salesforce can process batches of a job in parallel, and if your parent and child records sit in the same batch, you have no guarantee the parent row commits before the child row that references it.
The safe pattern is separate jobs, not separate batches within one job. Load all parent records in Job 1, wait for job completion with a status check, retrieve the generated Salesforce IDs, then map those IDs into your child record CSV before submitting Job 2. Skipping the completion check is the most common mistake we see. A job can report "InProgress" for several seconds after batches appear to finish, and submitting the dependent job too early reintroduces the exact race condition you were trying to avoid.
For three-level chains, such as Account to Opportunity to OpportunityLineItem-style custom objects, this means three sequential job submissions, each gated on the prior job's completion status. It is more orchestration than a flat load, but it is the only way to guarantee referential correctness with master-detail fields specifically, since unlike lookups you cannot patch the relationship after the fact with a quiet update.
Cascade delete and reparenting: what your test data has to cover
Master-detail relationships cascade deletes by default. Delete the parent and every detail record goes with it, including any grandchildren further down the chain. That behavior needs its own test coverage, separate from your insert testing, because cascade delete interacts badly with triggers, flows, and rollup recalculations that fire on the detail object.
Build a specific test scenario: insert a parent with 50+ detail children, delete the parent, and confirm the delete count matches, the related rollups clear correctly on any grandparent, and no orphaned detail records survive in a recycle-bin edge case. We have seen orgs where a before-delete flow on the detail object silently failed during cascade, leaving dangling records that only surfaced weeks later in a reporting discrepancy.
Reparenting is the second scenario worth testing deliberately. Salesforce allows reparenting on master-detail fields only if the field is marked as allowing reparenting in setup, and even then only under specific sharing and rollup conditions. Generate test data that attempts a reparent on a detail record with an active rollup summary pointing to a different master, and confirm the system either blocks it correctly or recalculates the rollup on both the old and new parent. This is an edge case that manual testing almost never catches because nobody reparents records by hand in a sandbox walkthrough.
Sequencing multi-level chains in Bulk API v2
A flat object list does not tell you load order. You need a dependency graph: which objects have master-detail fields, which objects those fields point to, and how many levels deep the chain runs. SproutEzee builds this graph directly from the org's schema metadata before generating a single row, so the load sequence is derived from the actual field definitions rather than from a hand-maintained script that drifts out of sync with the org.
Here is the pattern for a four-level chain, using a billing-style object model as an example:
- Job 1: Account (no master-detail dependency, root of the chain)
- Job 2: Billing_Agreement__c (master-detail to Account)
- Job 3: Invoice__c (master-detail to Billing_Agreement__c)
- Job 4: Invoice_Line__c (master-detail to Invoice__c)
Each job waits on the prior job's completion status, pulls the resulting Salesforce IDs, and injects them into the next CSV as foreign key references. For high-volume test runs, this adds real wall-clock time compared to a single flat load, but it is the only approach that avoids partial-failure cleanup. Partial failures on master-detail loads are expensive to unwind because you often cannot tell, from the error log alone, which detail records failed due to a missing master versus a genuinely bad value.
Sharing inheritance and why it needs its own test pass
Detail records inherit sharing from their master by default when the relationship is configured as "Controlled by Parent." That means your test data has to verify visibility, not just existence. A detail record can insert successfully and still be invisible to the test user running your validation queries, if the sharing model on the parent does not grant that user access.
This matters most in orgs that mix role hierarchy sharing with manual sharing rules on the parent object. Generate test users at different role levels, insert master-detail chains under records owned by a user at the top of the hierarchy, and confirm visibility cascades down correctly to detail records for users below. Skipping this check is how teams end up with a load job that reports 100% success and a QA pass that fails two days later because a support rep cannot see the invoice lines tied to a case.
Building a dependency-aware pipeline instead of a script you patch every sprint
The teams that get burned repeatedly by master-detail test data are almost always running a load script someone wrote eighteen months ago, hardcoded to a schema that has since picked up two new custom objects and a reparented field. Schema drift is constant in an active org, and a static script has no way to know a new master-detail relationship was added last sprint.
A schema-read approach solves this at the source. SproutEzee queries the org's object and field metadata directly, identifies every master-detail relationship including multi-level chains, and sequences the Bulk API v2 jobs automatically based on what it finds, not on what a script assumed six months ago. When a field gets converted from lookup to master-detail, or a new detail object gets added to an existing chain, the dependency graph picks it up on the next run without a manual rewrite.
That is the real argument for schema-driven generation over hand-built scripts: master-detail relationships are exactly the kind of constraint that is invisible until you violate it, and by then you have already burned a load window chasing REQUIRED_FIELD_MISSING errors across a partial batch.
Frequently Asked Questions
Why does my Bulk API v2 job fail only on master-detail fields and not on lookups?
Master-detail fields are required and non-nullable at insert time, while lookup fields accept a null or blank reference without error. If a child record's master-detail field points to a parent ID that does not yet exist, Salesforce rejects the row with a REQUIRED_FIELD_MISSING or invalid reference error. Lookup fields simply fail the row quietly or leave the reference blank, which is why ordering problems show up as hard failures only on master-detail relationships.
Can I load parent and child master-detail records in the same Bulk API v2 job?
No, not reliably. A single job can split its data into multiple batches that Salesforce may process in parallel, so there is no guarantee the parent batch commits before the child batch runs. The safe approach is two separate jobs: submit the parent job, poll until it reports a completed status, retrieve the generated record IDs, and only then submit the child job with those IDs mapped in.
What happens to detail records when I delete the master in test data?
Salesforce cascades the delete down through the entire master-detail chain by default, removing every detail and grandchild record tied to that master. This can interact badly with before-delete triggers or flows on the detail object, so cascade delete deserves its own test pass separate from insert validation. Confirm the delete count matches expectations and that any grandparent rollups recalculate correctly after the cascade completes.
Does reparenting work the same way on all master-detail fields?
No. Reparenting is only possible if the field is explicitly configured to allow it in Salesforce setup, and even then certain rollup summary and sharing configurations can block or alter the behavior. Test data should include a deliberate reparenting attempt on a detail record that has an active rollup summary, to confirm the rollup recalculates correctly on both the original and the new parent record.
How does schema-driven generation handle multi-level master-detail chains automatically?
A schema-driven tool reads the org's field metadata directly, identifies every master-detail relationship, and builds a dependency graph showing which objects must load before others. It then sequences Bulk API v2 jobs in that order automatically, mapping generated parent IDs into child records before each subsequent job runs. This removes the need for a hand-maintained load script that falls out of sync whenever a new object or relationship gets added to the org.