I have a child__c
object that has a lookup relationship to Parent__c
. Each have an ExternalId__c
field in them.
I am trying to create this relationship when inserting a child record.
List<Child__c> childList = new List<Child__c>();
for(wrapperObj wr : wrpList){
Child__c c = new Child__c();
string parentExtId = wr.Field1 + wr.Field2;
Parent__c prnt = new Parent__c(External_ID__c = parentExtId);
c.External_Id__c = wr.Field3 + wr.Field4;
c.Parent__r = prnt;
childList.add(c);
}
upsert childList External_Id__c;
Using this approach I sometime get an error when a Parent doesn't exists with the same parent external Id combination i.e. wr.Field1 + wr.Field2
. Error - INVALID_FIELD, Foreign key external ID:XXX not found for field External_ID__c in entity Parent__c
.Is there a way to mitigate this error. As of now I do not want to do a SOQL on Parent.