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
faceroy123faceroy123 

Bulkifying a trigger

Hi,

 

I have a trigger that sends an email once a field is updated (by a time based workflow).

Problem I have is that as time based workflows are processed in batches, this is only able to handle one update.

 

I guess I need to bulkify this trigger?! if so, how do I do that to the snippet below?

 

Thanks

 

trigger WorkflowEmailFromOwner on Task (before update) { 
                  
                if (trigger.new[0].escalated__c ==True && trigger.new[0].RecordTypeID =='012M00000008bZY' && trigger.new[0].number_of_updates__c ==0 ||(trigger.new[0].number_of_updates__c ==1)){
                Task tas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new];
                       try{ 
                messaging.Singleemailmessage mail=new 
messaging.Singleemailmessage(); 
      //Save the Email in Activity History 
      mail.setSaveAsActivity(false); 

 

Best Answer chosen by Admin (Salesforce Developers) 
Abhay AroraAbhay Arora
Hi ,
In such a case you can create a MAP of id and task and iterate with that task and send emails to a list of users at  once
1.) Create a map of id,task with all tasks for which you have to send email
2.) Itearate on above map create a list of emails
List<Messaging.SingleEmailMessage> email = new List<Messaging.SingleEmailMessage>();
3.) send out email to all uses  their respective emails
hope above will solve your problem please mark as solved if above works for you

faceroy123 wrote:

Hi,

 

I have a trigger that sends an email once a field is updated (by a time based workflow).

Problem I have is that as time based workflows are processed in batches, this is only able to handle one update.

 

I guess I need to bulkify this trigger?! if so, how do I do that to the snippet below?

 

Thanks

 

trigger WorkflowEmailFromOwner on Task (before update) { 
                  
                if (trigger.new[0].escalated__c ==True && trigger.new[0].RecordTypeID =='012M00000008bZY' && trigger.new[0].number_of_updates__c ==0 ||(trigger.new[0].number_of_updates__c ==1)){
                Task tas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new];
                       try{ 
                messaging.Singleemailmessage mail=new 
messaging.Singleemailmessage(); 
      //Save the Email in Activity History 
      mail.setSaveAsActivity(false);