• pmartin
  • 0 Points
  • Member since 2012
  • Technical Architect
  • Appirio

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1

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';

Custom_Object__c objTwo = new Custom_Object__c();
objTwo.Object_Self_Lookup__r = objOne;

insert parentList;
insert childList;