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
pinky1pinky1 

Opportunity: System.LimitException: Too many Email Invocations: 11

Need help:

I am sending am email when opportunity status is approved. My user is geeting below error. Opportunity: System.LimitException: Too many Email Invocations: 11. Please see my code below and help.

public class OpportunityAfterUpdateHandler implements TriggerDispatcher.ITriggerEntry {
    public void mainEntry(TriggerDispatcher.TriggerParameters tp) {
        List<Opportunity> opptyLstEmail = new List<Opportunity>(); //for email sending
        Map<Id, Opportunity> oldOppMap = (Map<Id, Opportunity>)tp.oldMap;

        for(opportunity opp : (List<Opportunity>)tp.newList) {
            opportunity oldopp = oldOppMap.get(opp.Id);
            system.debug('oldopp'+ oldopp);
            string recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opp.recordtypeid).getname();
            system.debug('recordtypename' + recordtypename);
            system.debug('Approved_Rejected__c' + opp.Approved_Rejected__c);
            system.debug('approval authority level' + opp.Approval_Authority_Level__c);


            if(recordtypename == 'Approved' && opp.Approved_Rejected__c=='Approved' && opp.Approval_Authority_Level__c != 'Manager' &&  (opp.Approved_Rejected__c != oldopp.Approved_Rejected__c))
            {
                opptyLstEmail.add(opp);
                system.debug('opptesting' + opp);
                Sendemailforoppty.SendingEmail(opptyLstEmail);
            }
            if(recordtypename == 'Other' && opp.Approved_Rejected__c=='Rejected' && opp.Approval_Authority_Level__c != 'Manager' &&  (opp.Approved_Rejected__c != oldopp.Approved_Rejected__c))
            {
                opptyLstEmail.add(opp);
                system.debug('opptesting' + opp);
                Sendemailforoppty.SendingEmail(opptyLstEmail);
            }
        }
    }

   
VinayVinay (Salesforce Developers) 
Hi Pinky,

You can only invoke 10 Send Emails per Context and you are hitting this is governor limit. 

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 only processes 10 contacts at a time.

Below are references that can give more information.
https://help.salesforce.com/articleView?id=000324660&type=1&mode=1

Batch class snippet:
http://forceguru.blogspot.com/2011/03/how-to-send-more-than-10-e-mails.html

https://salesforce.stackexchange.com/questions/154263/too-many-email-invocations-11
http://itsmersaini.blogspot.com/2014/11/systemlimitexception-too-many-email.html

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar