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, how to relationships

In the User's Guide there is a sample

of how to use the Test.loadData method to load a csv file with Accounts.


I cannot see any documentation nor samples showing how to load records with references (lookup or master-detail) to parent records - e.g. if I want to load Contacts, which refer to Accounts.


Anyone who knows. I suppose I am not going to "invent" record IDs????


Any update on this? I'm also looking for the same!


Basically, you need to use the same trick as you would do when inserting relational data in Apex i.e. using external ID.

For example, let's say we have a parent object called parent_obj__c


  • Define a custom field called External_ID__c and mark it as external id.
  • Make sure this field contains unique ids/text anything as long as its unique (it can also store same value as salesforce id for that record).


Let's say our child object is called Child_Object__c

  • Define a custom field on this object as well and call it External_ID_Child__c
  • Make sure that Child_Object__c.External_ID_Child__c = parent_obj__c.External_ID__c i.e. External_ID_Child__c contains its parent object external id value.


(one trick to do that is to create a formula field called formula_Ext_ID__c on child object which evaluates to parent_obj__r.External_ID__c)
When exporting this data from dataloader replace formula_Ext_ID__c column name with External_ID_Child__c.)


First load csv file containing parent
Then load csv file containing children records with External_ID_Child__c containing corresponding external id data.

Philip FPhilip F

 create the links (populate the parent ID in the child) manually in code to avoid adding unnecessary fields to the objects.



This method does not appear to work with Master-Detail.   When I insert the Child object I get the error that a required field is mssing.


How have you gotten around that?




It does work on master-child relationships. I've used it via code as well as via static resources. Where are you getting error, when attempting through code or through static resources?


I would love to see the code that works.  When I run the Test.loadData, it immediately loads the data straight into SF (though in test mode).   How do you define the mappting to use external ID or how do you preprocess the data?

King KooKing Koo
I agree with Steve, I am having the same problem now.  I just don't see how it would work with M/C relationship (would love to be proven wrong!!).  When you create a child record you need to specify a value for the field that you have set as the master/child field.  It cannot be NULL.  (same error message as Steve)

In the Account/Contact example, Account is not a required field on Contact so you can create the contact without specifying the account.

Hi All,

Just wanted to share my experience on this, one of my colleague(Mike) recently guided me how to do it. We can easily load records with relationships without the need of creating external ids. Insert all the test records as you do normally with a data loader once you are done with inserting, extract all the records separately object wise with necessary columns including salesforce ids and relationships fields for test.loaddata(). Now delete the records you inserted via UI or dataloader. 

Go ahead add all the saved csv files as static resources and try to use test.loaddata in the sequence based on the relationships defined. Now you see they work like a charm. 

Andrew RuckerAndrew Rucker
There's a simple solution to this. In the CSV file, create an Id column, and give the records unique Ids of your choosing (example: Account0, Account1, Contact0, Contact1, etc...). Then you can reference those Ids in fields such as ParentId on the Account object, or AccountId on the Contact object in your other Test.loadData() CSV files.

This is an undocumented feature of the Test.loadData() method. It's also why yvk431's solution would work.