Hi Guys,
SFDC is all about limits. When you get this error (System.LimitException: Too many Email Invocations: 11), you have exceeded a limit.
When we send email from Apex (Via calling messaging.SendEmail()), one call of this method is called one email invocation. And SFDC supports only 10 email invocations per context means You can only invoke 10 Send Emails per Context. That is governor limit.
Good news is, method sendEmail takes array of emails as a arguments, so we can collect all emailMessages and pass to this method to send all mails together. Example:
List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
//now do your loop
.... {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emailsLeads);
// mail.setCcAddresses(ccAddresses);
String subject='GoToMeeting Invitation Details for '+subject;
String body='test';
mail.setHtmlBody(body);
mail.setSubject(subject);
allMails.add(mail);
....
}
//Finished your loop? Now you have an array of mails to send.
//This uses 1 of your 10 calls to sendEmail that you are allowed
Messaging.sendEmail(allMails);
sendEmail can be called on an array of messages, so rather than call it once you should build up an array of SingleEmailMessage and then send them all in one call. You can debug email invocations like this.
System.debug('You have made ' + Limits.getEmailInvocations() + ' email calls out of ' + Limits.getLimitEmailInvocations() + ' allowed');
SFDC is all about limits. When you get this error (System.LimitException: Too many Email Invocations: 11), you have exceeded a limit.
When we send email from Apex (Via calling messaging.SendEmail()), one call of this method is called one email invocation. And SFDC supports only 10 email invocations per context means You can only invoke 10 Send Emails per Context. That is governor limit.
Good news is, method sendEmail takes array of emails as a arguments, so we can collect all emailMessages and pass to this method to send all mails together. Example:
List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
//now do your loop
.... {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emailsLeads);
// mail.setCcAddresses(ccAddresses);
String subject='GoToMeeting Invitation Details for '+subject;
String body='test';
mail.setHtmlBody(body);
mail.setSubject(subject);
allMails.add(mail);
....
}
//Finished your loop? Now you have an array of mails to send.
//This uses 1 of your 10 calls to sendEmail that you are allowed
Messaging.sendEmail(allMails);
sendEmail can be called on an array of messages, so rather than call it once you should build up an array of SingleEmailMessage and then send them all in one call. You can debug email invocations like this.
System.debug('You have made ' + Limits.getEmailInvocations() + ' email calls out of ' + Limits.getLimitEmailInvocations() + ' allowed');