How to insert child record using Parent record’s External Id field?

How to insert child record using Parent record’s External Id field?

Sample Code:

Hobby__c h = new Hobby__c(Name = ‘Test’);
h.Employee__r = new Employee__c(Employee_Primary_Email__c = ‘[email protected]’);
insert h;

here Employee__c is the field which is a lookup to Employee__c object and Employee_Primary_Email__c is the External Id field.

Cheers!!!

Leave a Reply