+ Start a Discussion

Trigger on Mass Emails

I have the following trigger on a Task... it works fine when entering one task at a time. 

However, when I send out mass emails, the activity is logged, but the trigger doesn't fire?


I read an older post that stated mass emails will not fire the triggers...

Is there a way to get the trigger to fire when a mass email is sent...


Below is the trigger...


trigger updatelastemail on Task (after insert) {
  Task t = Trigger.new[0];
  if(t.WhoId != null) {
    Lead l = [select Id, LastEmail__c from Lead where Id=:t.WhoId];
    if(l != null) {
      l.LastEmail__c = t.Email_Number__c;
      update l;


Any suggestions will be greatly appreciated.




Trigger code does not automatically process all records in a batch - you have to write the loops yourself to do this kind of bulk processing.  Your code right now processes specifically first record in the batch and then ends.


If you look for "bulk triggers" or "bulkify" in the various Apex documents and workbooks, you should find some good examples of how to write this code.


There is also a recording of a Dreamforce '09 session where bulk triggers was a main topic.  The title is "Best Practices for Force.com (Apex) Code: Lessons from the Trenches"