Friday 13 August 2021

How to Populate Salesforce Lookup Using an External ID?

Having External ID defined in an object will be very very helpful for data integration.

We can also use External ID to populate Look up field in Apex. 

Lets assume we have two Object MedicationStatement and HealthCondition. MedicationStatement has look up field to HealthCondition. Here is the code how to populate look up field value without writing query.

Diagnosis__c - Look up field from MedicationStatement to HealthCondition

DiagnosisId__c - External Id of HealthCondition object


HealthCondition cond = new HealthCondition(DiagnosisId__c = 123);

MedicationStatement medstat = new MedicationStatement();

medstat.PatientId = '0012g00000bZiaFAAS'; - This is Account Id

medstat.Status = 'Active';

medstat.MedicationCodeId = '0iP2g0000004C93EAE';

medstat.Diagnosis__r = 123;

medstat.OrderID__c = 55;

Insert medstat;

No comments: