+ Start a Discussion
SteveTechArcSteveTechArc 

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:

 

SF DOC:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

 

SF KNOWLEDGE ARTICLE

http://help.salesforce.com/apex/HTViewSolution?id=000167032&language=en_US

 

DISCUSSION BOARD

http://boards.developerforce.com/t5/Apex-Code-Development/Test-loadData-how-to-relationships/td-p/557477

 

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.  

 

#1  

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.

 

#2

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"

 

#3

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:

Product_Group__r\:Load_Key__c

Product_Group__r:Load_Key__c

Product_Group__r.Load_Key__c

PG_Load_key__c=Product_Group__r\:Load_Key__c

 

 

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?

Steve

 

 

RoyGiladRoyGilad
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,
Roy