How to populate relationship using External Id field in Salesforce?

I have read in the past that it is possible to insert parent and child records in a single DML operation using External Id field population for a lookup.

Today I tried to implement that but it took me some time to understand how to perform this correctly.

My first wrong try was to just put Id like in the case when parent record is already inserted.

new Cleaner_Job_Assignment__c(
    Cleaner__c = c.Id,
    Cleaning_Job__c = currentJob.Id
)

Apparently this failed with error

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CleaningOrderTrigger caused an unexpected exception, contact your administrator: CleaningOrderTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Cleaning Job]: [Cleaning Job]: Class.CleaningServices.createCleaningJobs: line 40, column 1

since I was trying to insert everything at the same time in one DML operation using the following code

List<SObject> recordsToInsert = new List<SObjec>();
recordsToInsert.addAll((List<SObject>) jobs);
recordsToInsert.addAll((List<SObject>) assignments);
insert recordsToInsert;

Obviously, the parent record hasn’t been inserted at the line where I tried to use its Id, that’s why it was null and that code failed with error that parent master relationship is not populated.

 

My second try was to put External Id field value into relationship field

new Cleaner_Job_Assignment__c(
    Cleaner__c = c.Id,
    Cleaning_Job__c = currentJob.Unique_Key__c
)

Apparently this also didn’t work. It failed with error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CleaningOrderTrigger caused an unexpected exception, contact your administrator: CleaningOrderTrigger: execution of AfterInsert caused by: System.StringException: Invalid id: a0058000004xG7PAAU_0: Class.CleaningServices.createCleaningJobs: line 28, column 1

My next try after reading documentation was to put entire parent object in full relationship field value:

new Cleaner_Job_Assignment__c(
 Cleaner__c = c.Id,
 Cleaning_Job__r = currentJob
)

This time I received another error namely “More than 1 field provided in an external foreign key reference in entity: Cleaning_Job__c”

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CleaningOrderTrigger caused an unexpected exception, contact your administrator: CleaningOrderTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Cleaning_Job__c: []: Class.CleaningServices.createCleaningJobs: line 39, column 1

So on the fourth attempt I decided to try to follow the documentation as much as possible and wrote the following snippet:

new Cleaner_Job_Assignment__c(
    Cleaner__c = c.Id,
    Cleaning_Job__r = new Cleaning_Job__c(
        Unique_Key__c = currentJob.Unique_Key__c
    )
)

Finally this time this version of code has worked.

Is this topic relevant to you? Feel free to express your impression in the comments.

Advertisements
This entry was posted in apex, lookup, Posts in English, record, salesforce and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s