You need to sign in to do that
Don't have an account?
Nadav Gross
Using triggers to create a relationship when upserting records using external id
Hi everyone,
In our implementation we have two custom objects: Platform_User__c and MRR_Movement__c. Platform_User__c has an Account_ID__c which serves as an external ID; MRR_Movement__c has an Account_ID__c to allow us to associate the two (but not defined as an external ID, since MRR Movements have a different external ID we use). We've also created Related_Platform_User__c as a master-detail relationship on MRR_Movement__c.
But to my surprise the following trigger, which I built based on the dev documentation on how to relate records using an external id, isn't working:
In our implementation we have two custom objects: Platform_User__c and MRR_Movement__c. Platform_User__c has an Account_ID__c which serves as an external ID; MRR_Movement__c has an Account_ID__c to allow us to associate the two (but not defined as an external ID, since MRR Movements have a different external ID we use). We've also created Related_Platform_User__c as a master-detail relationship on MRR_Movement__c.
But to my surprise the following trigger, which I built based on the dev documentation on how to relate records using an external id, isn't working:
trigger newMovement on MRR_Movement__c (before insert, before update) { for (MRR_Movement__c movement : Trigger.new) { Platform_User__c relatedUser = new Platform_User__c(Account_ID__c = movement.Account_ID__c); movement.Related_Platform_User__r = relatedUser; } }
This throws a required field missing error for Related_Platform_User__c.
I am pretty sure I'm missing something small and silly, please help a newbie out :)
Thanks for your message! I've actually tried that before posting, and it works; It's just that after reading the Salesforce article below I thought we may be able to do this without SOQL, since MRR movements will be added on a daily basis at bulk.
Here's the article that seems to suggest SOQL isn't needed:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_nested_object.htm
This works, but I am still looking for a way to do this without SOQL if anyone has any.