function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Test.loadData cannot load master detail records

I am trying to use Test.loadData to load parent and child records linked by the Master Detail relationship.


I have been referencing the follwoing:








However, none of these solve the problem.   Here is what I have done:


1)  Added an External ID field on both Parent and Child  "Load_Key__c"

2)  Created an Extra field on the Child, which holds the value of the External Id on the Parent. "PG_Load_Key__c"


Here is where things are not working.  



If I follow the examples, they instruct to do the Test.loadData with both Parent and Child.  Then, create a Map from the parent, setting the respective refernce.   This is where things fall down.  If I try the lodData on the child, with the Parent Reference empty, I get the error "Required fields are missing"


Therefore, I cannot get past the loadData on the child to add the references to the Parent.



I tried just putting the Parent's External ID value into the Mater Detail field of the child and I get the error:  "Invalid ID value"



I can load this data in the Data Loader using the External ID, (that does not help me with my test scripts).  I use a modified map file with the syntax: PG_Load_Key__c=Product_Group__r\:Load_Key__c


I have tried naming the field to see if it can leverage the same syntax, but it fails for these optons:







So, I am stuck.  I cannot load the child without the Parent Key.  I cannot modify the static resource file to put the key in at runtime.  I cannot find a way to have SF do the lookup on the External ID of the Parent.


How can anyone load Child (Master Detail) record with the Test.loadData()?


I have a complex object graph with about 8-10 objects with mutiple layered lookups and Master Details.


Has anyone accomplished this?




I came across the same issue, the problem is that Test.loadData function inserts the records to Salesforce DB, and you can't add \ change things before the insert operation.

Since I had already a factory class that handled it I re-used it and now I only use Test.loadData to manage my meta-data \ flat tables.

Sorry I could not help more,