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
RameshderRameshder 

How to write a batch apex class?

Description:
How to write batch apex class to fetch email id from contact for thant i want to send one e mail templates?

I want to schedule this batch apex at every day at 11 15

guys pls help me
Best Answer chosen by Rameshder
Apoorv Saxena 4Apoorv Saxena 4

Hi Rameshder,

Here you go:

In reference to your thread : https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XbHUQA0&feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&

Batch Class :
 

global class sendEmailToContacts implements Database.Batchable<sObject>{
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select id, Name,Email from Contact where Email=\'test@gmail.com\''); // you can modify the query as per your requirement.
    }
    
    global void execute (Database.BatchableContext BC, List<Contact> conList){
        EmailTemplate et=new EmailTemplate();
        et = [SELECT id FROM EmailTemplate WHERE developerName = 'case_closing'];
        
        List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
        for(Contact con:conList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setTargetObjectId(con.id);
            mail.setTemplateId(et.id);
            mail.setSaveAsActivity(false);
            mail.setSenderDisplayName('Your Name');
            mail.setReplyTo('Your Email');
            msgListToBeSend.add(mail);
        }
        Messaging.sendEmail(msgListToBeSend);
    }
    
    global void finish(Database.BatchableContext BC){      
        
    }
}

Now to Schedule you batch job, please follow the steps below:

Create a new Apex Class with the following code :

Apex Scheduler class :
 
global class SampleSchedulableClass implements Schedulable {
   global void execute(SchedulableContext SC) {
      sendEmailToContacts bp = new sendEmailToContacts();  // Creating a instance of your Apex Batch class that you want to execute
   }
}


Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
 
SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 15 11 * * ?';
System.schedule('Testing', cron, obj);

Now to check when your batch job is scheduled to run. Type 'Scheduled Jobs' in Quick Find Box and click on 'Scheduled Job' under Monitor section.You will be able to see when you batch job is scheduled.

Please mark this question as solved, if this helps you.

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4

Hi Rameshder,

Here you go:

In reference to your thread : https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XbHUQA0&feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&

Batch Class :
 

global class sendEmailToContacts implements Database.Batchable<sObject>{
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select id, Name,Email from Contact where Email=\'test@gmail.com\''); // you can modify the query as per your requirement.
    }
    
    global void execute (Database.BatchableContext BC, List<Contact> conList){
        EmailTemplate et=new EmailTemplate();
        et = [SELECT id FROM EmailTemplate WHERE developerName = 'case_closing'];
        
        List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
        for(Contact con:conList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setTargetObjectId(con.id);
            mail.setTemplateId(et.id);
            mail.setSaveAsActivity(false);
            mail.setSenderDisplayName('Your Name');
            mail.setReplyTo('Your Email');
            msgListToBeSend.add(mail);
        }
        Messaging.sendEmail(msgListToBeSend);
    }
    
    global void finish(Database.BatchableContext BC){      
        
    }
}

Now to Schedule you batch job, please follow the steps below:

Create a new Apex Class with the following code :

Apex Scheduler class :
 
global class SampleSchedulableClass implements Schedulable {
   global void execute(SchedulableContext SC) {
      sendEmailToContacts bp = new sendEmailToContacts();  // Creating a instance of your Apex Batch class that you want to execute
   }
}


Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
 
SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 15 11 * * ?';
System.schedule('Testing', cron, obj);

Now to check when your batch job is scheduled to run. Type 'Scheduled Jobs' in Quick Find Box and click on 'Scheduled Job' under Monitor section.You will be able to see when you batch job is scheduled.

Please mark this question as solved, if this helps you.

Thanks,
Apoorv
This was selected as the best answer
RameshderRameshder
User-added image
RameshderRameshder
Than u.....
Apporva
Please send me test class