I have a JSON coming from UI something like this. The object called Skill which has many external IDs for different reasons. There is one called EXTERNALID__c which i am making use for this scenario. NOTE – i am inserting multiple records of same object. its basically a self relation functionality.
{
"name":"branch1",
"parentId":"a27W00000026g5D", - Std salesfroce ID and I dont have problem here at all
"skillID":"NodeTier-2"
},
{
"name":"sub-branch2",
"parentId":"NodeTier-2",
"skillID":"NodeTier-3"
},{
"name":"leaf",
"parentId":"NodeTier-3",
"skillID":"NodeTier-4"
}
I tried something like this.
skill__c skill_1=new skill__c(Name='Test',EXTERNALID__c='CK-XXX');
skill__c skill_2=new skill__c(parent_branch__r=new skill__c(EXTERNALID__c='CK-XXX'), name='CK Test',EXTERNALID__c='CK-XXX-1');
insert new sobject[]{skill_1, skill_2};
However i am getting this error.[arent_branch__c is a look up field to same object. Any suggestions pls.
System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, Foreign key external ID: ck-xxx not found for field EXTERNALID_C__c in entity skill__C: []
If i want to perform this kind of operation in before trigger is it worth using external ID as a reference?
Best Answer
The object types cannot be the same. This functionality does not work for hierarchical relationships. See the below article in the Apex Developer Guide (emphasis mine):