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.

        "parentId":"a27W00000026g5D", - Std salesfroce ID and I dont have problem here at all

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?

  • It looks like you're calling all the inserts inside the one method. This won't work as until the method is finished, none of the record references are accessible inside the code. You'd have to insert them all then loop back through to update the parent Ids.
    – CaptainHaZ
    Mar 16, 2018 at 8:46
  • 1
    @Captain Normally that's incorrect and there's a better way, but with a self relationship you do need separate insert calls.
    – Adrian Larson
    Mar 16, 2018 at 9:21
  • Thanks All. Yes i saw the article and it says the sObject type must be different. let me try inserting individually and see if i can achieve without hitting any governor limits. The reason is i'm dealing with huge data. Mar 16, 2018 at 10:08

1 Answer 1


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):

Creating Parent and Child Records in a Single Statement Using Foreign Keys

You can use external ID fields as foreign keys to create parent and child records of different sObject types in a single step instead of creating the parent record first, querying its ID, and then creating the child record. To do this:

  • Create the child sObject and populate its required fields, and optionally other fields.
  • Create the parent reference sObject used only for setting the parent foreign key reference on the child sObject. This sObject has only the external ID field defined and no other fields set.
  • Set the foreign key field of the child sObject to the parent reference sObject you just created.
  • Create another parent sObject to be passed to the insert statement. This sObject must have the required fields (and optionally other fields) set in addition to the external ID field.
  • Call insert by passing it an array of sObjects to create. The parent sObject must precede the child sObject in the array, that is, the array index of the parent must be lower than the child’s index.

You can create related records that are up to 10 levels deep. Also, the related records created in a single call must have different sObject types. For more information, see Creating Records for Different Object Types in the SOAP API Developer Guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.