Sunday 6 January 2013

Run Batch Class Hourly

16:34
This is a use case that we all come across very often, Schedule a batch class every hour to clean up data or to send out batch emails to case team members (Which I’ll blog about later).
There are three main steps involved in this
  1. Write a Batch class with the required logic
  2. Write a Scheduled Apex which calls the above Batch Class
  3. Schedule the class from the developer console by executing anonymous apex
Step 1: Write the batch class
Batch apex gives us the advantage to run jobs that might require a lot more that the usual governor limit contexts, example Batch job are made to perform common UPSERT operation on a scheduled basis. The Batch apex, can be used to conveniently perform time to time task and some real complex job ranging from data cleansing, archiving the data to the other quality improvements

Learn more about Batch Apex here.

Example of a batch class

global class ExampleBatchClass implements Database.Batchable<sObject>
{
    global ExampleBatchClass(){
              // Batch Constructor
     }
   
     // Start Method
     global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
     }
   
     // Execute Logic
     global void execute(Database.BatchableContext BC, List<sObject> scope){
            // Logic to be Executed batch wise      
   
     }
   
     global void finish(Database.BatchableContext BC){
   
     }
   
 }

Step 2: Write the Schedulable class

Now, We have the batch class ready and it has to be in a schedulable context in-order to schedule the batch. You can learn more about schedulable apex here

Example of a Scheduled Apex

  global class scheduledBatchable implements Schedulable{
     global void execute(SchedulableContext sc) {
        // Implement any logic to be scheduled

           // We now call the batch class to be scheduled
        ExampleBatchClass b = new ExampleBatchClass(); 
        
        //Parameters of ExecuteBatch(context,BatchSize)
        database.executebatch(b,10);
     }
  }


Step 3: Schedule the class by executing anonymous

Finally now we can schedule the batch class, there are two ways by which we can schedule the batch class


  1. From Setup—> Apex Classes –> Schedule Apex : but, here the minimum is one day/ 24 hours
  2. By executing anonymous code from either developer console or apex, here the minimum is 1 hour

Code to be executed

// Cron EXP for hourly schedule
   String CRON_EXP = '0 0 * * * ?';
   SheduledBatchable sch = new scheduledBatchable();
   system.schedule('Hourly Example Batch Schedule job', CRON_EXP, sch);

If you want to run it as frequent as 15,30 or N mins .....

System.schedule('Job1', '0 * * * * ?', new SchJob());
System.schedule('Job2', '0 15 * * * ?', new SchJob());
System.schedule('Job3', '0 30 * * * ?', new SchJob());
System.schedule('Job4', '0 45 * * * ?', new SchJob());


We’re all done ! now you can see your batch job scheduled Setup—>Monitoring –> Scheduled Jobs

would love to hear your thoughts , Happy Coding !