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
34213421 

System.LimitException: Too many Email Invocations: 11

Hi I have a batch class below. Upon executing I found the following execution error. Can anyone help me with this?
List<Orde__cr> ordr = new List<Order__c>();
 global void execute(Database.BatchableContext BC, List<Order__c> scope){
     
		system.debug('scope'+scope.size());  
     List<Order__c> fbdge = [select Id,Description__c,Type__c,Contact__c,
 			Contact__r.Expiration_Date__c,Contact__r.FirstName,Contact__r.LastName,
        	Contact__r.Years__c,OrderApi__Contact__r.Email
   From Order__c where (Type__r.Name ='AAA' or Type__r.Name ='BBB') and Active__c =true];
        			
     if (fbdge!=null){
 
        
for(Order__c bd: fbdge)
{
    
 if(bd.Contact__r.Years__c>=10.00 && (bd.Contact__r.Expiration_Date__c<=Date.newInstance( 2017, 6, 30 )) ){  
   						 
     			 List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                                 List<String> email = new List<String>();
                                 List<String> ccemail = new List<String>();                                 
                              
   			 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     						
     							email.add(bd.Contact__r.Email);
    							 Contact c = [select id, Email from Contact where email <> null limit 1];
     							
     EmailTemplate templateId = [Select id from EmailTemplate where name = 'Test1'];
    
                                            mail.setToAddresses(email);
                                          
                                           	mail.setTargetObjectId(c.Id);
           									mail.setTreatTargetObjectAsRecipient(false);
                                           	mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);
                                            system.debug(mail);
									
     Messaging.sendEmail(mails);
        						system.debug('mails');
        system.debug(mails.size());
                                
 } else if (bd.Contact__r.Years__c>10 && (bd.Contact__r.Expiration_Date__c<=Date.newInstance( 2017, 6, 30 )) ){
       
     							email.add(bd.Contact__r.Email);
                                
    							 Contact c = [select id, Email from Contact where email <> null limit 1];
     	
     EmailTemplate templateId = [Select id from EmailTemplate where name = 'Test2'];
     
                                            mail.setToAddresses(email);
                                            mail.setccAddresses(ccemail);
                                          
                                           	mail.setTargetObjectId(c.Id);
           									mail.setTreatTargetObjectAsRecipient(false);
                                           	mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);
                                            system.debug(mail);

                            
 } else if (bd.Contact__r.Years__c<10 && (bd.Contact__r.Expiration_Date__c > Date.newInstance( 2017, 6, 30 )) ){
    

     							email.add(bd.Contact__r.Email);
                                
    							 Contact c = [select id, Email from Contact where email <> null limit 1];
     
     EmailTemplate templateId = [Select id from EmailTemplate where name = 'Test3'];
     
                                            mail.setToAddresses(email);
                                          
                                          
                                           	mail.setTargetObjectId(c.Id);
           									mail.setTreatTargetObjectAsRecipient(false);
                                           	mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);
                                          
 

                                
 }
     Messaging.sendEmail(mails);
        						system.debug('mails');
        system.debug(mails.size());
     }

 
Raj VakatiRaj Vakati

 

Move the below code into the finish method. Because you are invoking email trigger logic into every record . which was wring 
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Test3'];
                                            mail.setToAddresses(email);
                                           	mail.setTargetObjectId(c.Id);
                                                mail.setTreatTargetObjectAsRecipient(false);
                                           	mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);
 }
     Messaging.sendEmail(mails);
     }

 

or the second option is. move email logic as show below  


List<Orde__cr> ordr = new List<Order__c>();
 global void execute(Database.BatchableContext BC, List<Order__c> scope){
                   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    system.debug('scope'+scope.size());  
     List<Order__c> fbdge = [select Id,Description__c,Type__c,Contact__c,
             Contact__r.Expiration_Date__c,Contact__r.FirstName,Contact__r.LastName,
            Contact__r.Years__c,OrderApi__Contact__r.Email
   From Order__c where (Type__r.Name ='AAA' or Type__r.Name ='BBB') and Active__c =true];
                    
     if (fbdge!=null){
 
        
for(Order__c bd: fbdge)
{
    
 if(bd.Contact__r.Years__c>=10.00 && (bd.Contact__r.Expiration_Date__c<=Date.newInstance( 2017, 6, 30 )) ){  
                            
                  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                                 List<String> email = new List<String>();
                                 List<String> ccemail = new List<String>();                                 
                              
                             
                                 email.add(bd.Contact__r.Email);
                                 Contact c = [select id, Email from Contact where email <> null limit 1];
                                 
     EmailTemplate templateId = [Select id from EmailTemplate where name = 'Test1'];
    
                                            mail.setToAddresses(email);
                                          
                                               mail.setTargetObjectId(c.Id);
                                               mail.setTreatTargetObjectAsRecipient(false);
                                               mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);
                                            system.debug(mail);
    
 } else if (bd.Contact__r.Years__c>10 && (bd.Contact__r.Expiration_Date__c<=Date.newInstance( 2017, 6, 30 )) ){
       
                                 email.add(bd.Contact__r.Email);
                                
                                 Contact c = [select id, Email from Contact where email <> null limit 1];
         
     EmailTemplate templateId = [Select id from EmailTemplate where name = 'Test2'];
     
                                            mail.setToAddresses(email);
                                            mail.setccAddresses(ccemail);
                                          
                                               mail.setTargetObjectId(c.Id);
                                               mail.setTreatTargetObjectAsRecipient(false);
                                               mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);
                                            system.debug(mail);

                            
 } else if (bd.Contact__r.Years__c<10 && (bd.Contact__r.Expiration_Date__c > Date.newInstance( 2017, 6, 30 )) ){
    

                                 email.add(bd.Contact__r.Email);
                                
                                 Contact c = [select id, Email from Contact where email <> null limit 1];
     
     
     }
     }
     
     if(mails.size()>0){
      Messaging.sendEmail(mails);
 
                                system.debug('mails');
 
        system.debug(mails.size());
 
     }
     }

  


 
34213421
I did try option 2,

But now the issue I have is I see different receipients from each loop printed, but the single is email is sent thrice. The toadress for each email is having 3 different receipients (combined of all three if conditions). I did try using email.clear() inside the for loop, but that's only sending the email now for first recipient of first loop three times.