You need to sign in to do that
Don't have an account?
Mont Mont
MassEmailMessage vs “Too many Email Invocations: 11”
I don't know, how to fix it: my messages are in List.
Helper:
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?
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
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