0

while am executing this code am getting this error as single email limit exceeded. An Apex error occurred: System.EmailException: SendEmail failed. First exception on row 0; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: [] code:

public class Idfattachment
{
@InvocableMethod
public static void Idfattach(List<Id> InventionDisclosure)
{
       IDF_Email__c settings = IDF_Email__c.getInstance('EmailConfig');
        String status= settings.status__c;
        String tosub = settings.subject__c;
        String toemail = settings.Email__c;
        Boolean taction = settings.Mail_ON_OFF__c;
        if(taction)
        {
            List<Messaging.SingleEmailMessage> myListofMails = NEW List<Messaging.SingleEmailMessage>();
            Map<string,List<ContentDocumentLink>> MapIDFsDocLink = NEW Map<string,List<ContentDocumentLink>>();
            set<id> ConLinkIDset = New set<id>();
            list<id> idfid = New list<id>();  
            List<ContentDocumentLink> lstContentDocumentLinks = [SELECT ContentDocumentId,Id,LinkedEntityId
                                                            FROM ContentDocumentLink WHERE LinkedEntityId IN: InventionDisclosure];
            for(ContentDocumentLink ConDocLink : lstContentDocumentLinks)
            {
                ConLinkIDset.add(ConDocLink.ContentDocumentId);
                List<ContentDocumentLink> temp = MapIDFsDocLink.get(ConDocLink.LinkedEntityId);
               if(temp == null) 
               {
                   MapIDFsDocLink.put(ConDocLink.LinkedEntityId, new List<ContentDocumentLink>{ConDocLink});
               } 
               else 
               {
                   temp.add(ConDocLink);
               }
            }
            Map<string,List<ContentVersion>> MapLinksDocuments = NEW Map<string,List<ContentVersion>>();
            List<ID> ConLinkIDSList = NEW List<ID>(ConLinkIDset);
            List<ContentVersion> documents= [ 
                                                SELECT Id, Title,PathOnClient,VersionData,isLatest,ContentDocumentId
                                                FROM ContentVersion
                                                WHERE isLatest = true AND ContentDocumentId  IN:ConLinkIDSList
                                            ];
            for(ContentVersion ConDoc : documents)
            {
                List<ContentVersion> temp = MapLinksDocuments.get(ConDoc.ContentDocumentId);
                if(temp == null) 
                {
                    MapLinksDocuments.put(ConDoc.ContentDocumentId, new List<ContentVersion>{ConDoc});
                } 
                else 
                {
                    temp.add(ConDoc);
                }
            }
            List<Invention_Disclosure_New__c> idf1 = [Select Id,Title__c,Disclosure_Status__c from Invention_Disclosure_New__c where Id in : InventionDisclosure];
            for(Invention_Disclosure_New__c idf : idf1)
            {   
                    if(idf.Disclosure_Status__c == status)
                    {
                        Id newDocLink = idf.Id;
                        if(MapIDFsDocLink.containsKey(idf.id))
                        {
                            set<ID> lstDocId = new set<ID>();
                            List<ContentDocumentLink> lstContentDocumentLinks1 =MapIDFsDocLink.get(idf.id);               
                            for(ContentDocumentLink docit : lstContentDocumentLinks1) 
                            {                   
                                lstDocId.add(docit.ContentDocumentId);  
                            }                   
                            List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>{};
                            List<ContentVersion> documents1= NEW List<ContentVersion>();
                            for(id ids : lstDocId)
                            {
                                if(MapLinksDocuments.containsKey(ids))
                                {
                                    List<ContentVersion> documentslist = MapLinksDocuments.get(ids);
                                    documents1.addall(documentslist);
                                }                                   
                            }
                            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();                
                            for (ContentVersion document: documents1)
                            {
                                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
                                attachment.setBody(document.VersionData); 
                                attachment.setFileName(document.PathOnClient);
                                attachments.add(attachment);
                            }
                            semail.setSubject(tosub);
                            string query ='SELECT Id,'+toemail+' FROM Invention_Disclosure_New__c WHERE Id =:newDocLink';
                            List<Invention_Disclosure_New__c > sobjList = Database.query(query);
                            String[] sendTo = new String[]{}; 
                            Set<string> fieldApis = New Set<string>();
                            List<String> lstAlpha = toemail.split(',');
                            if(lstAlpha.size()>0)
                            for(string s:lstAlpha)
                            {
                                fieldApis.add(s);
                            }
                            for(Invention_Disclosure_New__c  tmpUser:sobjList)
                            {
                            if(fieldApis.contains('PPA_EmailId__c'))
                                if(tmpUser.PPA_EmailId__c!=null && tmpUser.PPA_EmailId__c!='')
                                    sendTo.add(tmpUser.PPA_EmailId__c);
                                    system.debug('PPA email:'+sendTo);
                                if(fieldApis.contains('Inventor_Email_Id__c'))
                                    if(tmpUser.Inventor_Email_Id__c!=null && tmpUser.Inventor_Email_Id__c!='')
                                        sendTo.add(tmpUser.Inventor_Email_Id__c);
                                        system.debug('Inventor email:'+sendTo);
                            }
                            semail.setToAddresses(sendTo);
                            system.debug(semail);
                            String messageBody = '<html><body>Hi,New Invention Disclosure entitled '+idf.Title__c +',has been submitted for review.Please refer the attached document for the complete information of the submitted disclosure.</body></html>';
                            semail.setHtmlBody(messageBody); 
                            semail.setFileAttachments(attachments);
                            if(attachments.size() > 0)
                            {
                                myListofMails.add(semail);
                            }
                        }
                }                       
            }
            if(myListofMails.size()>0) 
            {
                Messaging.sendEmail(myListofMails);
            }
        }

  }
 }
4
  • There is a limit on the number of emails you can send from Apex, this is 5,000 per 24 hours. You should apply debug to check the size like this System.debug('List Size '+ idf1.size()) just after line 51 query. check logs then.
    – Mr.Frodo
    Commented May 26, 2018 at 8:45
  • List size is 1only Commented May 26, 2018 at 8:59
  • Ok add debug System.debug('myListofMails'+ myListofMails.size()); before Messaging.sendEmail(myListofMails); Line 121.
    – Mr.Frodo
    Commented May 26, 2018 at 9:03
  • again size 1 only Commented May 26, 2018 at 9:12

1 Answer 1

1

Change Last part with this code:

if(myListofMails.size()>0) 
{
    try {
        Messaging.reserveSingleEmailCapacity(myListofMails.size());
        Messaging.sendEmail(myListofMails);
    } catch (Exception e) {
        // From Winter '14, this code is now executed.
        System.debug('You have used up your daily allowance of mails');
    }
}

It will check if you have reached your daily limit which is 5000 emails per day and not send mails than.

3
  • class runned without error,but mail not sended.got exception in debug as DEBUG|You have used up your daily allowance of mails Commented May 26, 2018 at 9:24
  • @SFDCDeveloper you cannot send emails now. Thats it done for the day. Ask Salesforce to increase your daily email limits or use 3rd party email provider. Commented May 26, 2018 at 9:27
  • Yes @SFDCDeveloper that might be the case.
    – Mr.Frodo
    Commented May 26, 2018 at 9:27

Not the answer you're looking for? Browse other questions tagged .