You need to sign in to do that
Don't have an account?
Patrick G. Brown
Trigger to add master record id based on values from detail record id
I have a trigger to create a Monthly Outreach record that looks to be correct, but I continue to run into a required field error. Here is an example of my error:
I have a master record (Outreach Target) with two identical values (see below) that I'm manually adding to this child record for the purposes of matching. Those two fields are Location ID and Year Submitted. My trigger is attempting to add a parent Outreach Target record ID the child Monthly Outreach record by querying the Outreach Target object and searching for a record with matching criteria. The criteria is unique, so my SOQL query will only find one record.
Here is the master record who's Id I'm attempting to add to my child Monthly Outreach record:
Here is my trigger:
I've even tried hard-coding the master record ID into the query to guarantee I'm finding the matching record, but I continue to get the required field error. Can anyone please help me solve this issue?
I have a master record (Outreach Target) with two identical values (see below) that I'm manually adding to this child record for the purposes of matching. Those two fields are Location ID and Year Submitted. My trigger is attempting to add a parent Outreach Target record ID the child Monthly Outreach record by querying the Outreach Target object and searching for a record with matching criteria. The criteria is unique, so my SOQL query will only find one record.
Here is the master record who's Id I'm attempting to add to my child Monthly Outreach record:
Here is my trigger:
trigger SetOutreachTarget on Monthly_Outreaches__c (after insert, after update) { for (Monthly_Outreaches__c mo : Trigger.new) { String moy = mo.Year__c; String moa = mo.Location_ID__c; List<Outreach_Target__c> ot = [SELECT Id FROM Outreach_Target__c WHERE Outreach_Target_Year__c = :moy AND Location_ID__c = :moa]; if(ot.size()>0){ mo.Outreach_Target__c = ot[0].Id;} } }
I've even tried hard-coding the master record ID into the query to guarantee I'm finding the matching record, but I continue to get the required field error. Can anyone please help me solve this issue?
Please check the latest code:
Here I considered:
(1) Naming Convention.
(2) Alignment.
(3) Bulkified the trigger.
(4) Added comments.
Note: I couldn't test this as I don't have full setup of above objects in my org.
Please do let me know if it helps you.
Regards,
Mahesh
You did several things to improve my code and that is extremely helpful. Thanks again for taking a look at it.
Please find the modified code:
Please do let me know if it helps you.
Regards,
Mahesh
Do you know any tricks to get around this? I've read about other ways to circumvent this, but none of them really apply to my situation.