You need to sign in to do that
Don't have an account?
Setting external Id for same-object-lookup on Insert
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.
Thanks!
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;
You shouldn't need to insert the parent in order to setup the relationship. The external id should be good enough for that, as long as the parent is inserted first. In fact you should be able to create the parent, create the child with a lookup to the parent, put them both into an sobject list (parent first) and insert in one go.
The trick I've found is that if you simply create the parent and populate the name and external id, that fails as the name and external id are considered to be fields that can be match on and you get an error. Thus to create an account and contact in one go, I had to create the account with name/external id populated, then create the child the with the relationship set to a new version of the account with only the external id field set.
The code is as follows:
All Answers
The last post on this thread is what I've been modeling my code from, for what it's worth:
http://boards.developerforce.com/t5/Perl-PHP-Python-Ruby-Development/Setting-a-lookup-field-using-an-External-ID-in-UPSERT/td-p/88604/page/2
Matt,
should the insert parentLine;
be just after setting the parent fields
since the parent hasn't been inserted - there isn't the relationship yet.
Hey Peter - In this simplified example, perhaps, but in the real example I'm running through some big loops, doing a bunch of branching logic, creating these records in unique batches based on some conditions.
If there are 100 parent records, there will be 300 child records, 3 children per parent. I've got to set the Parent Id during the loops.
But I think you're right, given that I'm setting the reference to be objOne, and objOne doesn't exist until the insert, it's not getting set. Hrm.
You shouldn't need to insert the parent in order to setup the relationship. The external id should be good enough for that, as long as the parent is inserted first. In fact you should be able to create the parent, create the child with a lookup to the parent, put them both into an sobject list (parent first) and insert in one go.
The trick I've found is that if you simply create the parent and populate the name and external id, that fails as the name and external id are considered to be fields that can be match on and you get an error. Thus to create an account and contact in one go, I had to create the account with name/external id populated, then create the child the with the relationship set to a new version of the account with only the external id field set.
The code is as follows:
The sample code I believe is correct (just need to do the insertion of the parent first).
So your logic ends up being something like this?
The only thing I can think of here is that you insert your parents, then loop through the parents and create your children. So effectively it looks like this:
Complete guess btw:)
Matt give this a try.
i"ll dm you my address for the six pack!
Thanks everyone! bob_buzzard's solution of instantiating a new object to set the reference worked perfectly!
Just for documentation, here's the code I ended up using (in concept) that worked: