Tuesday, April 30, 2019

How to schedule batch class in salesforce

Benefits of batch apex
The execution logic of the batch class is called once for each batch of records. The default batch size is 200 records. You can also specify
a custom batch size. Furthermore, each batch execution is considered a discrete transaction. With each new batch of records, a new set
of governor limits is in effect. In this way, it’s easier to ensure that your code stays within the governor execution limits. Another benefit
of discrete batch transactions is to allow for partial processing of a batch of records in case one batch fails to process successfully, all
other batch transactions aren’t affected and aren’t rolled back if they were processed successfully.

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)

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());

No comments:

Post a Comment