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
angelchangelch 

Too many email invocations: 11

Hi all,

I have developed a trigger (before insert, before update) on Lead object that calls a method inside a class for sending a list of emails. The trigger is fired when a batch filter and process a big amount of records uploaded through Data Loader. Depending on the number of records inserted on Lead object, email limit is reached (System.LimitException: Too many Email Invocations: 11). 

I have seen in the debug log that the trigger is fired too many times, but it has been written following best practices advices to avoid this kind of issue. It seems to be records are been processed in packages of 200. In fact, when I try to insert more than 400 records the exception is shown (I'm using also some workflow rules that cause the trigger is executed one more time). I have read about it in other forum discussions, but the new API theoretically solved this problem for Bulk triggers. 

Could you give me any advice to solve this problem?

Thanks in advance.
Edgar MoranEdgar Moran
You should use Mass email instead Single Email class:
 
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.saveAsActivity = false;
   
mail.setTargetObjectIds(mailToIds);
mail.setTemplateId(e.Id);
           
 mail.setUseSignature(false);
mail.setSaveAsActivity(false);


// Send the email
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });

and use a List<Messaging.MassEmailMessage> to set the email information and send an array in the send Email.

Hope this helps