You need to sign in to do that
Don't have an account?
Lahiru Bandara
External Id issue::: INVALID_FIELD, Foreign key external ID: extid::3073631 not found for field
I'm getting above error message when I try to create child records based on the External Id of the Parent Record. Here is the scenario. Please note that both reference1 and reference2 are related to the same Master object (Master_record__c). And reference1 is Master-Detail relationship and reference2 is just a lookup to Master record
String aExtId = helper.createMasterRecord(param1, param2);//Master Record (Master_record__c) creates with the External Id Child_rec__c aRec = new Child_rec__c(); aRec.reference1__r = new Master_record__c(External_Id__c = aExtId); aRec.reference2__c = 'a2w2C0000005vaVQAQ'; objectsToInsert.add(aRec); //Same List has the Master record to be inserted too.
Here's what happens inside the createMasterRecord()
Master_record__c aMaster = new Master_record__c(); aMaster.Name = 'Test'; code.. code.. aMaster.External_Id__c = 'random text'; return aMaster.External_Id__c;
Any idea how to fix this?
String aExtId = helper.createMasterRecord(param1, param2);//Master Record (Master_record__c) creates with the External Id Child_rec__c aRec = new Child_rec__c(); aRec.reference1__r = new Master_record__c(External_Id__c = aExtId); aRec.reference2__c = 'a2w2C0000005vaVQAQ'; objectsToInsert.add(aRec); //Same List has the Master record to be inserted too.
Here's what happens inside the createMasterRecord()
Master_record__c aMaster = new Master_record__c(); aMaster.Name = 'Test'; code.. code.. aMaster.External_Id__c = 'random text'; return aMaster.External_Id__c;
Any idea how to fix this?
To understand the issue better, I did some search and found this bloghttp://bobbuzzard.blogspot.sg/2012/03/create-parent-and-child-records-in-one.html which talks about a simialr situation where the contact and account are inserted using the same insert statement.
Your code seems to be correct, only thing you need to make sure is that this list you are inserting has the master inserted first and then the child as shown in this sample code below:
Account acc=new Account(Name='Blog Acc 8', Master_Id__c='Blog Acc 8');
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', Account=new Account(Master_Id__c='Blog Acc 8'));
insert new List<Sobject>{acc, cont};