Populate Lookups Using External Id from APIs

ย
We all might have used External id field, which is useful when we need to identify records uniquely based on an external system identifiers. We can use these fields while creating or updating records to avoid creating any duplicates in normal scenarios.
Did you know that you can use the same external id field to populate lookupsโ
That is, an external field on parent object can be used to populate the relation when creating/updating a child object record from API.
Lets break this down by an example:
- ๐Lets assume i have 2 objects
๐๐ข๐ณ๐ฆ๐ฏ๐ต๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ค
and๐๐ฉ๐ช๐ญ๐ฅ๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ค
related by a lookup relationship. - ๐
๐๐ข๐ณ๐ฆ๐ฏ๐ต๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ค
has an external Id field"๐๐ถ๐ฃ๐๐บ๐ด๐ต๐ฆ๐ฎ๐๐ฅ__๐ค"
which hold Id from an external system. - ๐I have a
๐๐ข๐ณ๐ฆ๐ฏ๐ต๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ค
record with Name=“๐๐ข๐ณ๐ฆ๐ฏ๐ต001” and ๐๐ถ๐ฃ๐๐บ๐ด๐ต๐ฆ๐ฎ๐๐ฅ__๐ค=“๐๐๐๐๐1”
My objective is to create a new record of ๐๐ฉ๐ช๐ญ๐ฅ๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ค
with parent as record “๐๐ข๐ณ๐ฆ๐ฏ๐ต001”.
Normally we would use Id of parent record to populate this relationship.But mostly this salesforce id may not be available for External system. That is when we can use ๐๐ถ๐ฃ๐๐บ๐ด๐ต๐ฆ๐ฎ๐๐ฅ ,External Id field from parent to populate the “๐๐ข๐ณ๐ฆ๐ฏ๐ต๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ณ” relationship on child!โ
๐This can be used for update using patch method as well
Endpoint : /๐ด๐ฆ๐ณ๐ท๐ช๐ค๐ฆ๐ด/๐ฅ๐ข๐ต๐ข/๐ท60.0/๐ด๐ฐ๐ฃ๐ซ๐ฆ๐ค๐ต๐ด/๐๐ฉ๐ช๐ญ๐ฅ๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ค
Method: ๐๐ฐ๐ด๐ต
Body:{ "๐๐ข๐ฎ๐ฆ":"๐๐ฉ๐ช๐ญ๐ฅ๐๐ฆ๐ค๐ฐ๐ณ๐ฅ001", "๐๐ข๐ณ๐ฆ๐ฏ๐ต๐๐ฃ๐ซ๐ฆ๐ค๐ต__๐ณ": { "๐๐ถ๐ฃ๐๐บ๐ด๐ต๐ฆ๐ฎ๐๐ฅ__๐ค":"๐๐๐๐๐1" } }
Below is a demo of the same with the help of Salesforce Rest Explorer VS Code Extension!๐ฉ๐ช
watch demo video on linkedin
SF documentation on Relating Records by Using an External ID in Apex
This Content was originally posted in linkedin View Post