[SalesForce] Inserting Records using external ID

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

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.