You need to sign in to do that
Don't have an account?
sfdc1.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;
}
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;
}
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
-
It figures out which Eval_Request__c records are needed.
-
It queries those Eval_Request__c records.
-
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
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>
-Glyn
I compiled the code and got an error: "Variable does not exist: map_EvalRequests" on line 23.
-Glyn
Question: is it best practice to check to accommadate values in fields that are null no matter what?
-Glyn