Update relationships using External IDs

Let’s suppose we have two objects, Speaker object and another one called company.

The speaker record below has “Marc_Benioff” as External Id and it is related to a company called “SFDC”.


Its company looks as follows:

We want to update the speaker’s company to another one called Microsoft


We can do it without using any SFDC ID, by using its External ID. You can achieve it by using the following code:

Speaker__c speaker = new Speaker__c();
// we set the External Id of our speaker record
speaker.external_id__c = 'Marc_Benioff';

// We have to specify the External ID of the company.
// Notice that we are updating the relationship (__r)
speaker.company__r = new Company__c(External_id__c = 'Microsoft');

// We upsert our speaker record using its External Id as well
upsert speaker Speaker__c.Fields.External_Id__c;

This is just an example, we all want Marc Benioff to be leading SFDC.com forever 🙂

Leave a Reply

Notify of