function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc1.3890582999007844E12sfdc1.3890582999007844E12 

bulkify trigger

New to APEX and need help bulkifying this trigger.  The Eval_Request__c is the parent object of Eval_Unit_Details__c.  Thanks in advance!!!

trigger Update_Email_on_Eval_Detail on Eval_Unit_Details__c (before insert) {

    Eval_Unit_Details__c EUD = trigger.new[0];{
        Eval_Request__c ER = [select shipping_contact_email__c from Eval_Request__c where id=:EUD.eval_request__c];
        EUD.Shipping_Contact_Email__c=ER.Shipping_Contact_Email__c;
    }
Best Answer chosen by sfdc1.3890582999007844E12
GlynAGlynA
<pre>
trigger Update_Email_on_Eval_Detail on Eval_Unit_Details__c (before insert)
{
    //  loop through the records in the trigger to build a set of related Eval Request IDs
    Set<Id> set_EvalRequestIDs = new Set<Id>();
    for ( Eval_Unit_Details__c detail : trigger.new )
    {
        if ( detail.Eval_Request__c != null ) set_EvalRequestIDs.add( detail.Eval_Request__c );
    }

    //  query the Eval Requests and put the results into a Map
    Map<Id,Eval_Request__c> map_EvalRequests = new Map<Id,Eval_Request__c>
    (   [   SELECT  Id, Shipping_Contact_Email__c
            FROM    Eval_Request__c
            WHERE   Id IN :set_EvalRequestIDs
        ]
    );

    //  loop through the records in the trigger again to modify the email addresses
    for ( Eval_Unit_Details__c detail : trigger.new )
    {
        if ( detail.Eval_Request__c == null ) continue;
        detail.Shipping_Contact_Email__c
                = map_EvalRequests.get( detail.Eval_Request__c ).Shipping_Contact_Email__c;
    }
}
</pre>

All Answers

GlynAGlynA
The code in the following post should work for you.  The code works in three steps:
  1. It figures out which Eval_Request__c records are needed.
  2. It queries those Eval_Request__c records.
  3. It modified the Shipping_Contact_Email__c fields of the records in the trigger.
I apologize if there are any typos - I was not able to compile the code.  Let me know if you have any questions.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Twitter: @GlynAtClosedWon
GlynAGlynA
<pre>
trigger Update_Email_on_Eval_Detail on Eval_Unit_Details__c (before insert)
{
    //  loop through the records in the trigger to build a set of related Eval Request IDs
    Set<Id> set_EvalRequestIDs = new Set<Id>();
    for ( Eval_Unit_Details__c detail : trigger.new )
    {
        if ( detail.Eval_Request__c != null ) set_EvalRequestIDs.add( detail.Eval_Request__c );
    }

    //  query the Eval Requests and put the results into a Map
    Map<Id,Eval_Request__c> map_EvalRequests = new Map<Id,Eval_Request__c>
    (   [   SELECT  Id, Shipping_Contact_Email__c
            FROM    Eval_Request__c
            WHERE   Id IN :set_EvalRequestIDs
        ]
    );

    //  loop through the records in the trigger again to modify the email addresses
    for ( Eval_Unit_Details__c detail : trigger.new )
    {
        if ( detail.Eval_Request__c == null ) continue;
        detail.Shipping_Contact_Email__c
                = map_EvalRequests.get( detail.Eval_Request__c ).Shipping_Contact_Email__c;
    }
}
</pre>
This was selected as the best answer
GlynAGlynA
BTW:  If the Eval_Request__c lookup field is guaranteed to exists (because it's a Master-Detail or otherwise required), you can get rid of the tests for null on lines 07 and 21.

-Glyn
sfdc1.3890582999007844E12sfdc1.3890582999007844E12
Thank you very much for your quick reply GlynA!

I compiled the code and got an error: "Variable does not exist: map_EvalRequests" on line 23.
GlynAGlynA
Hmmm.  map_EvalRequests is defined on line 11.  Could you check the spelling and make sure that both identifiers (on lines 11 and 23) are spelled the same?

-Glyn
sfdc1.3890582999007844E12sfdc1.3890582999007844E12
Hi GlynA, my mistake I removed line 7 and 21 cause I was pretty sure that there wouldn't be any null values, but I was wrong.  It works perfectly now.  Thank you very much!

Question: is it best practice to check to accommadate values in fields that are null no matter what?
GlynAGlynA
I make a habit of checking for and skipping null values "just in case" - especially when I'm going to insert them into a set or map, because null is a perfectly valid element in a set or as a key in a map.  But if you put null in a set and then query "WHERE somefield IN :theset", you're going to get every record that has "somefield == null".  This has been the source of some hard-to-find bugs for me in the past.

-Glyn