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
RohanSharma8791RohanSharma8791 

Create a batch class which send email to the account with the list of all related contacts which were created in last 7 days along with date of creation

I want to write a batch class which send email to the account with the list of all related contacts which were created in last 7 days along with date of creation .
Best Answer chosen by RohanSharma8791
CharuDuttCharuDutt
Hii Karan
Try Below Code That WIll Fetch Last 7 Days Record
Last_N_Days:8public class LeadProcessor implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id,Name,Email,(Select Id,Name From Contacts) FROM Account Where CreatedDate = Last_N_Days:7');
    }
    public void execute(Database.BatchableContext bc, List<Account> records){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
         List<String> sendTo = new List<String>();
        for(Account acc :records){
      String s ='';
            sendTO.add(Acc.EmailAdress);
        for(Contact Con :acc.contacts){
            s+=Con.Name + '====>' + Con.CreatedDate + ',';
        }
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Account Related Contacts');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            
        acc.Description =  s.removeEnd(',');
    	}
        Messaging.SendEmail(mails);
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
 

All Answers

CharuDuttCharuDutt
Hii Karan
Try Below Batch
public class LeadProcessor implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id,Name,Email,(Select Id,Name From Contacts) FROM Account');
    }
    public void execute(Database.BatchableContext bc, List<Account> records){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
         List<String> sendTo = new List<String>();
        for(Account acc :records){
      String s ='';
            sendTO.add(Acc.EmailAdress);
        for(Contact Con :acc.contacts){
            s+=Con.Name + '====>' + Con.CreatedDate + ',';
        }
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Account Related Contacts');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            
        acc.Description =  s.removeEnd(',');
    	}
        Messaging.SendEmail(mails);
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
RohanSharma8791RohanSharma8791
Hey CharuDutt, Thanks for a quick response.
I have a question that will it fetch records for the last 7 days or the whole list of contacts?
CharuDuttCharuDutt
Hii Karan
Try Below Code That WIll Fetch Last 7 Days Record
Last_N_Days:8public class LeadProcessor implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id,Name,Email,(Select Id,Name From Contacts) FROM Account Where CreatedDate = Last_N_Days:7');
    }
    public void execute(Database.BatchableContext bc, List<Account> records){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
         List<String> sendTo = new List<String>();
        for(Account acc :records){
      String s ='';
            sendTO.add(Acc.EmailAdress);
        for(Contact Con :acc.contacts){
            s+=Con.Name + '====>' + Con.CreatedDate + ',';
        }
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Account Related Contacts');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            
        acc.Description =  s.removeEnd(',');
    	}
        Messaging.SendEmail(mails);
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
 
This was selected as the best answer