Thursday, August 3, 2017

Batch apex scheduling in apex - batch apex job run for every 15 minutes or 30 minutes

We can schedule a batch apex using declarative way/OOTB, for this we need to write the schedule class that calls the batch apex class. Using declarative way we can schedule the class either weekly or monthly basis.

In weekly, we can have it run every day or any selected day of week. For a particular day we can specify a preferred time at which the class run. So, using declarative way we can have a highest frequency of 1 time per day not more than that. That is it does not support to run the class more number of times per day. Say, you want the class to run every 1 hour, or every 30 mins, then you cannot do it using declarative way. To do this, we can use system.schedule method and call the scheduler class. 

System.schedule takes 3 parameters as below :

system.schedule('jobname',timeperiod,scheduler class name)

jobname: you can give any name to your job
timeperiod: specify the time frequency with which the job should run
scheduler class name: scheduler class that calls your batch apex class.

timeperiod should be specified in below format:
Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

For example, to run every 30 mins the expression would be : 0 30 * * * ?

here '*' means every time against the specified parameter, every hour, every day of month, every day of week and '?' means no specific value. Thus, this expression means every 30 minutes. So, the system.schedule('testname',0 30 * * * ?,schedulerclass()); would call the schedulerclass every 30 minutes when the statement executes.

You can visit salesforce documentation to know more on system.schedule parameters.


Lets write a simple batch apex class, its scheduler class and then, execute system.schedule method to run that batch apex every 30 minutes.
Batch Apex Class:

global class BatchApexDemo implements database.batchable<sobject>{
Public string soqlquery;
 Public void setQry(string soqlquery){
    this.soqlquery = 'Select name,status from account limit 1';
 }
 global database.querylocator start(database.batchableContext bc){
  return database.getquerylocator(soqlquery);
 }
 global void execute(database.batchablecontext bd, list<sobject> sc){
   System.debug('**In Execute Method**');
 }
 Public void finish(database.batchableContext bc){
  
 }
}

Scheduler class along with method that will Can scheduler class every 30 minutes:



Scheduler class along with method that will Can scheduler class every 30 minutes:
global with sharing class ScheduleBatchApexDemo implements Schedulable {
global void execute(SchedulableContext sc) {
  ID BatchId = Database.executeBatch(new BatchApexDemo(), 200);
}

Public static void SchedulerMethod() {
  string timeinterval = '0 30 * * * ?';
  System.schedule('BatchApexDemo-Every15mins',timeinterval, new ScheduleBatchApexDemo());
  }
}


Now, we have to just execute the "SchedulerMethod" once, this will keep on calling the batch apex every 30 minutes. We can use developer console to execute this method. Just execute below statement to call the method: ScheduleBatchApexDemo.SchedulerMethod();



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.