Loading

'Too many API email invocations 11' error when sending emails via Apex

Julkaisupäivä: Oct 13, 2022
Kuvaus
There is no email limit from Apex. The limit is on the number of times the sendEmail() method can be invoked from Apex.  We will not be having a limit for Emails from Apex, however, we will be having the limit for number of times the sendEmail() method can be invoked from Apex in a transaction. 

 

Best practice is to ensure that you're not calling the sendEmail() method inside a for loop

Ratkaisu

 

Sample code for SendEmail()

NOTE: The code provided below is an example. You should always test code before pushing to production.

//method to send mail to a list of users
public static void DispatchEmail() {
    List lstMails = new List();
      lst = 20;
       for(Integer i : lst) {
          Messaging.SingleEmailMessage message = getEmail();       
 //Construct the email message here by populating the values such as body, subject
                //Adding emails to the list       
 lstMails.add(message);
    }  
   //Sending the email list in a single call.
    Messaging.sendEmail(lstMails);
  }

 
public static Messaging.SingleEmailMessage getEmail() {
    Messaging.SingleEmailMessage mailMessage = new Messaging.SingleEmailMessage();
    mailMessage.setSaveAsActivity(false);
    return mailMessage;
  }
Knowledge-artikkelin numero

000385111

 
Ladataan
Salesforce Help | Article