Hi all - I've got a custom object for which there are two basic kinds of records, parents and children. There is a same-object Lookup field on that object that looks up to the object called Object_Self_Lookup__c. I want to use this to relate the children to the parents.
The object also has an external Id called External_Id_Field__c.
I'm generating large batches of these records in a scheduled Apex class, and maintaining two different lists, one of parents, one of children. Each record, no matter the type, is assigned a unique value to External_Id_Field__c.
My goal is that I can insert the list of Parent records first, having set all their external ids, and then insert the list of children, and have the lookup to the parent record resolved by way of the external Id. I've like to avoid adding a trigger, or making a second pass to get the reference set if it's possible.
Is this possible? If so, what am I doing wrong here? This is simplified version of my full code, but this doesn't work either.
List<Custom_Object__c> parentList = new List<Custom_Object__c>(); List<Custom_Object__c> childList = new List<Custom_Object__c>(); Custom_Object__c objOne = new Custom_Object__c(); objOne.External_Id_Field__c = '1234'; parentList.add(objOne); Custom_Object__c objTwo = new Custom_Object__c(); objTwo.Object_Self_Lookup__r = objOne; childList.add(objTwo); insert parentList; insert childList;