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.

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