You need to sign in to do that
Don't have an account?
micchris
Test.loadData, how to relationships
In the User's Guide there is a sample http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm?CSHID=apex_methods_system_test.htm#TestLoadDataExampleSection
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
Let's say our child object is called Child_Object__c
(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.
Here's an alternative to skaus' solution from the Salesforce documentation that you may find helpful.
Personally, I opted to 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?
Steve
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?
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.
--yvk
This is an undocumented feature of the Test.loadData() method. It's also why yvk431's solution would work.