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
Mont MontMont Mont 

MassEmailMessage vs “Too many Email Invocations: 11”

I don't know, how to fix it: my messages are in List.

Helper:
public with sharing class Helper {
    
    public static void afterInsert(List<P__c> newList, Map<ID, P__c> newMap){
    
    	List<Id> userIds = new List<Id>();
        EmailTemplate emailTemplate = [SELECT Id FROM emailTemplate WHERE Name = 'Email'];
        Messaging.Email[] messages = new Messaging.Email[0];
        
        for(User u : [SELECT Id FROM User WHERE UserRole.Name = 'Manager']){
                userIds.add(u.Id);
            }
    
    	for(P__c p : newList){
            
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
  	    mail.templateId = emailTemplate.Id;
            mail.targetObjectIds = userIds;
            mail.saveAsActivity = false;
  	    messages.add(mail);
    	}
        Messaging.sendEmail(messages);
    }
Trigger:
trigger EmailTrigger on P__c (after insert) {
   
    if (Trigger.isAfter && Trigger.isInsert) {
        Helper.afterInsert(Trigger.new, Trigger.newMap);
    } 
}
Bulk-test:
@isTest
    static void sendEmailBulkTest(){
        insert createUser();

        for(Integer i=0; i<11; i++){
            insert p();
        }
        Integer emailInvocations = Limits.getEmailInvocations();
        system.assertEquals(emailInvocations, 11);
        
    }
And I get msg "System.LimitException: Too many Email Invocations: 11". How can I fix it?

 
Raj VakatiRaj Vakati
Try this code
 
public with sharing class Helper {
    
    public static void afterInsert(List<P__c> newList, Map<ID, P__c> newMap){
    
    	List<Id> userIds = new List<Id>();
        EmailTemplate emailTemplate = [SELECT Id FROM emailTemplate WHERE Name = 'Email'];
        Messaging.Email[] messages = new Messaging.Email[0];
        
        for(User u : [SELECT Id FROM User WHERE UserRole.Name = 'Manager']){
                userIds.add(u.Id);
            }
    
    	for(P__c p : newList){
            
            Messaging.SingleEmailMessage  mail = new Messaging.SingleEmailMessage ();
  	    mail.templateId = emailTemplate.Id;
            mail.targetObjectIds = userIds;
            mail.saveAsActivity = false;
  	    messages.add(mail);
    	}
        Messaging.sendEmail(messages);
    }

 
Amit Chaudhary 8Amit Chaudhary 8
Resolution:
There is no 10 email limit from Apex. The limit is on the number of times the SendEmail() method can be invoked from Apex. We need to ensure that we are not calling SendEmail() method inside a for loop.

Try to update your test class like below
@isTest
    static void sendEmailBulkTest(){
        insert createUser();

        List<P> lstP = new List<P>();

        for(Integer i=0; i<11; i++){
            lstP.add(p);
        }
        insert lstP();
        Integer emailInvocations = Limits.getEmailInvocations();
        system.assertEquals(emailInvocations, 11);
        
    }

NOTE:- issue is coming because you are inserting P record in loop. Never use DML inside the for loop.

Let us know if this will help you