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
VirijaVirija 

SINGLE_EMAIL_LIMIT_EXCEEDED, Failed to send email: []:

Hi,

 

Below is the snippet of the code that I am using to send single e-mails to the users in my organization. For the first we hit a daily limit of 500 e-mails and the users are unable to approve the process and also receoive the e-mails. My requirement is not to send external e-mails but to send e-mails to users in my organization. Do i have to make any changes in the code to avoid hitting the limit?

 

I came to know that the single e-mail limit will be hit only when we try to send e-mail to external e-mail addresses. Do I have to make any changes in my query to prevent this scenario?

 

Thank You in advance for the help.   

 

 

public static void sendEmailToProgTeam(Id objId,string status,Id progId,string OpsBillingName)
    {
        OpsBillingproc__c TestToBeUpdated = new     OpsBillingproc__c();
        TestToBeUpdated = [Select Id,name,ApprovalComments__c,customownerid__c from  OpsBillingproc__c where id=:objId];
        
        
        List<ProgramTeamMember__c> lstProgTeam = new List<ProgramTeamMember__c>();
        List<User> lstUser = new List<USer>();
        Set<Id> setUserId = new Set<Id>();
        
        lstProgTeam = [select id,TeamMemberId__c,RelatedprogramId__c from ProgramTeamMember__c where RelatedprogramId__c = : progId]; 
         if(lstProgTeam!=null)
         {
             for(ProgramTeamMember__c obj : lstProgTeam)
             {
                  setUserId.add(obj.TeamMemberId__c);
             }
         }
         
         Program__c objProg = [select id,ownerid from Program__c where id = : progId];
         
         if(objProg !=null)
         {
             setUserId.add(objProg.ownerid);
         }
         lstUser = [select id,name,email from User where id IN : setUserId];        
         
         List<String> toAddress= new List<String>();
         
          string body='';
          
          for(User u : lstUser)
          {
               toAddress.add(u.email);
          }        
          
          //if S&M approver rejectes the record
          if(Status == 'Complete')
          {
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                     
                         body ='<html><body><pre>';
                         body += 'Dear Users';
                         body += '<br/><br/>This is to inform you that the OpsBilling Record \''+ OpsBillingName +'\' has been Approved.';
                         body += '<br/><br/>Approval Comments : '+TestToBeUpdated.ApprovalComments__c;
                         body += '<br/></br>Kind Regards';          
                         body += '<br></br>Salesforce App Team';
                        mail.setSubject('OpsBilling Record Approved');                    
                        mail.setToAddresses(toAddress);                                      
                        mail.sethtmlbody( body );                       
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });                     
                        
          }            
         

sfdcfoxsfdcfox
Using setToAddresses is what's using up your daily limits. Use setTargetObjectId(s), passing in a user id or list of user IDs. Check the docs for details.
VirijaVirija

Thank You for the prompt response. I have corrected my code (marked in red). Is this the change that we need to avoid limit?

 

Thank You. 

 

public static void sendEmailToProgTeam(Id objId,string status,Id progId,string OpsBillingName)
    {
        OpsBillingproc__c TestToBeUpdated = new     OpsBillingproc__c();
        TestToBeUpdated = [Select Id,name,ApprovalComments__c,customownerid__c from  OpsBillingproc__c where id=:objId];
        
        
        List<ProgramTeamMember__c> lstProgTeam = new List<ProgramTeamMember__c>();
        List<User> lstUser = new List<USer>();
        Set<Id> setUserId = new Set<Id>();
        
        lstProgTeam = [select id,TeamMemberId__c,RelatedprogramId__c from ProgramTeamMember__c where RelatedprogramId__c = : progId]; 
         if(lstProgTeam!=null)
         {
             for(ProgramTeamMember__c obj : lstProgTeam)
             {
                  setUserId.add(obj.TeamMemberId__c);
             }
         }
         
         Program__c objProg = [select id,ownerid from Program__c where id = : progId];
         
         if(objProg !=null)
         {
             setUserId.add(objProg.ownerid);
         }
         lstUser = [select id,name,email from User where id IN : setUserId];        
         
         List<String> toAddress= new List<String>();
         
          string body='';
          
          for(User u : lstUser)
          {
               toAddress.add(u.id);
          }        
          
          //if S&M approver rejectes the record
          if(Status == 'Complete')
          {
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                     
                         body ='<html><body><pre>';
                         body += 'Dear Users';
                         body += '<br/><br/>This is to inform you that the OpsBilling Record \''+ OpsBillingName +'\' has been Approved.';
                         body += '<br/><br/>Approval Comments : '+TestToBeUpdated.ApprovalComments__c;
                         body += '<br/></br>Kind Regards';          
                         body += '<br></br>Salesforce App Team';
                        mail.setSubject('OpsBilling Record Approved');                    
                        mail.setTargetObjectId(toAddress);                                      
                        mail.sethtmlbody( body );                       
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });                     
                        
          }            

sfdcfoxsfdcfox
Yes, that solution should work, according to the documentation. I've not used it yet myself, so I can't confirm this.
VirijaVirija
Thank you for the reply. I will try it out and will let yo know.
haripriya.maturiharipriya.maturi

How it is possible ? without specifying setToAddresses.  I did same way you suggested, but  I got this error REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc)

 

can you suggest me to overcome SINGLE_EMAIL_LIMIT_EXCEEDED error

 

Thanks and Regards,

Haripriya.