Integration

Update Records using External ID - Salesforce Rest API

Update Records using External ID - Salesforce Rest API (Populating Lookup using External ID)

Update Records using External ID - Salesforce Rest API



In this post, we will see how we can update a record based on External Id using Salesforce Rest API and how to populate Lookup fields using External ID

Begin with creating an ExternalID field on Account object. Let's call the text field as Ext. See snapshot below.

Update Records using External ID - Salesforce Rest API


Click next and grant access to the profiles and save the field.

once done go ahead and create an account record as shown in the snapshot

Update Records using External ID - Salesforce Rest API


The external Id for this account is set as "WOS". Now we would try to update this record using rest API with the help of external ID field.

click on REST Explorer to open the Rest explorer tool. Enter the credentials and click on login. Make sure you select the correct environment.

Update Records using External ID - Salesforce Rest API

Now in the screen provided choose the PATCH method,
change the rest URL to  /services/data/v39.0/sobjects/Account/Ext__c/WOS

this specifies that we want to upsert account records based on Ext__c field as External Id field where the value of external ID is WOS.
Type anything in the name field and click Execute.

Update Records using External ID - Salesforce Rest API

Check back in salesforce and you would see that the record name is changed.

You can even use the external ID fields to fill up the lookup field. For example, if you want to create a contact and want to link it to an account.  The external system which is creating the contact using rest API does not know the Account's salesforce ID but has the external Id. In these cases, we can link the account using external ID.

In the same Rest Explorer change the method to POST and  change the endpoint URL to  /services/data/v39.0/sobjects/contact
and use below JSON in the body to create a contact record.

{
"lastName": "Demo Contact",
"Account": {"Ext__c":"WOS"}
}

Click execute and you would see a contact record Id generated in the response.  Check this contact in Salesforce and you would see that it is linked to the account whose External id field has WOS as the value.

** For Custom Lookup fields use __r instead of __c.

Populating Lookup using External ID


About Saurabh Dua

0 comments:

Post a Comment

Powered by Blogger.