0

Below is my batch apex code,in this query if i put limit 1 - the mail is sending successfully . if i don't put any limit in query - mail sending failed .if query gets 5 records also -mail sending failed.how to achieve this.

global class DMSalertmail implements Database.Batchable<sObject>
{
global Database.querylocator start(Database.BatchableContext BC)
{
   string query = 'SELECT Id,CreatedDate,Next_30_Days__c,Title FROM ContentVersion where Next_30_Days__c=53 ';
    return Database.getQueryLocator(query);
}

public List<ContentVersion> myContentVersion =new List<ContentVersion>();
global String body='';
global void execute(Database.BatchableContext BC, List<ContentVersion> scope)
{
    for(ContentVersion cv : scope)
    {
        myContentVersion.add(cv);
        system.debug(myContentVersion);
    }

    List<Messaging.SingleEmailMessage> lstEmail = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage mail1=new Messaging.SingleEmailMessage();
    String[] toadd=new String[]{'[email protected]'};

    for(ContentVersion cv :myContentVersion)
    {
        if(cv.Next_30_Days__c!=Null && cv.Next_30_Days__c==53)
        {
            string b = 'File Name:'+cv.Title +' '+'This file have created 30 days back.';
             body += b ;
            mail1.setToAddresses(toadd);
            mail1.setSubject('DMS alert Mail');
            mail1.setHtmlBody(body);
            lstEmail.add(mail1);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1 });
            System.debug(body);
        }
    }
    Messaging.SendEmail(lstEmail);
    system.debug(lstEmail);
}
global void finish(Database.BatchableContext BC)
{

}}

1 Answer 1

1

I think you need to work on that for loop. Since you're not instantiating new object for every email you want to send, same instance gets added in the list. Also Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1 }); should be removed since you're already adding all those emails in lstEmail

List<Messaging.SingleEmailMessage> lstEmail = new List<Messaging.SingleEmailMessage>();

String[] toadd=new String[]{'[email protected]'};

for(ContentVersion cv :myContentVersion)
{
    if(cv.Next_30_Days__c!=Null && cv.Next_30_Days__c==53)
    {
        Messaging.SingleEmailMessage mail1=new Messaging.SingleEmailMessage();
        string b = 'File Name:'+cv.Title +' '+'This file have created 30 days back.';
         body += b ;
        mail1.setToAddresses(toadd);
        mail1.setSubject('DMS alert Mail');
        mail1.setHtmlBody(body);
        lstEmail.add(mail1);
    }
}
Messaging.SendEmail(lstEmail);
system.debug(lstEmail);
2
  • how to send more mails...
    – Elango
    Commented Sep 26, 2017 at 8:59
  • Mass email can only be sent to an address in a standard email field. Mass email using a custom email field is not supported. Instead of forming email body using Apex, try using visualforce email template and set whatId and toaddress accordingly. Commented Sep 26, 2017 at 9:33

You must log in to answer this question.

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