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
EllaElla 

Email error

I have a scheduled flow to run daily : 

1. A get element that will take all my contacts under specific criteria : 
User-added image2. I loop through all the contacts found: 
User-added image

3. I assign the IDs of the loop into a variable.
User-added image
4. I have an APEX class to send an email and the last action is to send the email to the contacts. 
public class SendIMRMonthlyEmails {
    
    @InvocableMethod
    public static void invokeapex(list<Project_Contacts__c>ProjectContacts){
        List<Id> contactIds = new List<Id>();
        List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
        EmailTemplate et=[Select id from EmailTemplate where name = :System.Label.Sent_IMR_Monthly_Email_Project_Contacts limit 1];
        
        for(Project_Contacts__c obj : ProjectContacts){
            if(obj.Project_Deliverables__c=TRUE){
                contactIds.add(obj.Id);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId(obj.Project_Contact__c);
                mail.setSenderDisplayName('IMR Monthly Emails');
                mail.setTemplateId(et.id);
                mail.setSaveAsActivity(FALSE);
                mail.setWhatId(obj.Id);
                mail.setBccAddresses(new List<String> {System.Label.Sent_IMR_Monthly_Email_BCC_Email});
                emailList.add(mail);      
        }   
    }
        Messaging.sendEmail(emailList);
    }  
}


Still, when I ran the flow I receive an error: "
An Apex error occurred: System.LimitException: Too many Email Invocations: 11" which leads me to the idea that I did something wrong to the flow or to the apex class. 

User-added image
mukesh guptamukesh gupta
Hi Ella,

You can only invoke 10 Send Emails per Context. That is governor limit. You are hitting 11 emails and its bombing.
 
To fix this, you can create a Batch Apex Class that your Schedule Apex invokes. Move the Send Email logic inside your Batch Apex class and scope the batch so that is only processes 10 contacts at a time.
 
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_gov_limits.htm

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

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

Thanks
Mukesh 
EllaElla
Hi Mukesh, 

I am afraid that I don't know how to move the logic into the apex class. I am new into this . 

TY,
Valeria,