Scott Covert

Many times I have run into the situation where I would like to insert a parent record and its children into Salesforce’s database simultaneously. This can be a little tricky if you are inserting a list of parent records and would like to instantiate the children records prior to the commit of said list because then you do not yet have the Salesforce Ids for the children records to reference. External Id fields to the rescue!

Instead of instantiating the children with the lookup relationship field being set to the parent record’s Id you can define the reference field to be set equal to an empty record of the parent’s sObject type that was instantiated with the same external Id field value as the parent record the child should lookup to:

Parent__c p = new Parent__c(Name=’ABC’,External_Id__c = ‘123’,Description__c=’ABCs and 123s’);

Parent__c refToP = new Parent__c(External_Id__c=’123’);

Child__c c = new Child__c(Parent__r = refToP);

insert p;

insert c;

Oddly enough, you have to create the empty record refToP that is instantiated with the external id field value of p — you cannot simply set c’s reference field Parent__r equal to p itself. That said, you do not have to actually insert the empty record refToP into your database-so no harm, no foul.

Thanks to the following site for helping me find this solution:

Cascade Insert with External Id Fields - Engineering