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
idiris Abdukhamitov 1idiris Abdukhamitov 1 

I have a method that send mass Email. So how can I test it and be sure that its sending exact number of email what I want?

Method:
list<Id> contactIds
Messaging.MassEmailMessage GG = new Messaging.MassEmailMessage();
GG.setTemplateId(emailtemplate1.Id);
GG.setTargetObjectIds(contactIds); Messaging.sendEmail(new Messaging.MassEmailMessage[] { GG });
mukesh guptamukesh gupta
Hi Idiris,

you need to use 
 
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);


for(Messaging.SendEmailResult  result : results ){

if (result.success) 
{
    System.debug('The email was sent successfully.');
} else 
{
    System.debug('The email failed to send: ' + result.errors.message);
}

}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
idiris Abdukhamitov 1idiris Abdukhamitov 1
@mukesh gupta how I can use it on test class?
 
mukesh guptamukesh gupta

Hi Idiris,

Please use below code:-
 
Test.startTest();
    yourmethodThatSendsEmail();
    Integer invocations = Limits.getEmailInvocations();
Test.stopTest();

system.assertEquals(1, invocations, 'An email should be sent');

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 
idiris Abdukhamitov 1idiris Abdukhamitov 1
@mukesh gupta but i need number of emails