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
Kunal Purohit 4Kunal Purohit 4 

How to write batch apex code for following scenario

I want to send mail with Total_Paper_Published value. Total_Paper_Published__c is a rollup summary field.
Here is the code. Plz suggest correction.
public class EmailBatch implements database.Batchable<sobject>,Schedulable{
public decimal count=0;
    public void execute(Schedulablecontext sc)
    {
        EmailBatch eb=new EmailBatch();
    database.executeBatch(eb);
    }
    
    public database.QueryLocator start(database.BatchableContext bc)
    {
        string query='select Name,Journal_Email_Address__c,Total_Published_Paper__c from Account';
        return database.getQueryLocator(query);
    }
    
    public void execute(database.BatchableContext bc,list<Account> acc)
    {
       list<Account>alist=new list<Account>();
        for(Account a:acc)
        {
         count=a.Total_Published_Paper__c;
        
    }
    list<Messaging.SingleEmailMessage> mlist=new list<Messaging.SingleEmailMessage>();
        messaging.SingleEmailMessage email=new messaging.SingleEmailMessage();
        email.setSubject('Welcome');
        email.setPlainTextBody('Total Paper Published' +count);
        string[] to=new string[]{'kunalpurohit7@gmail.com'};
            email.setToAddresses(to);
        mlist.add(email);
        messaging.SendEmail(mlist);
        system.debug('Mail sent Successfully');
    }        
    public void finish(database.BatchableContext bc)
    {
//system.debug(count);        
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Kunal,

Can you try the below code:
 
global class EmailBatch implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    global Integer count = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'select Name,Journal_Email_Address__c,Total_Published_Paper__c from Account'
        );
    }
    global void execute(Database.BatchableContext bc, List<Account> scope){
         list<Account>alist=new list<Account>();
        for(Account a:acc)
        {
         count=count+a.Total_Published_Paper__c;
        
    }
	
	Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string[] to = new string[] {kunalpurohit7@gmail.com};
        string[] cc = new string[] {ccMail};
        
        email.setToAddresses(to);
        if(ccMail!=null && ccMail != '')
	        email.setCcAddresses(cc);
        if(repmail!=null && repmail!= '')
        	email.setInReplyTo(repMail);
        
        email.setSubject('Test Mail');
        
        email.setHtmlBody('Hello,'+count );
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        
        system.debug('Mail sent Successfully');
    }    
    global void finish(Database.BatchableContext bc){
        System.debug('email sent');
    }    
}

For executing the batch class you can use the below lines in anonymous window:
 
EmailBatch myBatchObject = new EmailBatch(); 
Id batchId = Database.executeBatch(myBatchObject);
Additionally, I would suggest you have a look at the below trailhead module for information and in case if you want to make sure that this run at a scheduled interval then you can use the above lines in a schedulable apex implementation and make sure it runs as per the time mentioned.

Let me know if there is any need for help.

Regards,
Anutej
 
ANUTEJANUTEJ (Salesforce Developers) 
Trailhead Module: https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_batch