Author Archives: Biswajeet

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.

Schedule Batch Apex For Every Hour

Batch Class:

global class accountBatch implements Database.Batchable<sobject> {
  
    global Database.QueryLocator start(Database.BatchableContext bc){
      
        String query = 'SELECT Id, Name FROM Account';
        return Database.getQueryLocator(query);
    }
      
    global void execute(Database.BatchableContext bc, List<account> scope) {
      
        for(Account a : scope) {
            a.Name = a.Name + 'Updated';
        }
        update scope;
    } 
      
    global void finish(Database.BatchableContext bc) {
      
    }
}

Scheduled Class:

global class accountBatchSchedule implements Schedulable{
	global void execute(SchedulableContext sc) {
		//invoke the batch class
        Database.executeBatch(new accountBatch());
    }
}

Execute below code from anonymous window in developer console. This will scheduled the job to run in every hour starting from 12:00 AM.

accountBatchSchedule sc = new accountBatchSchedule();
//Define cron expression
String cronExp = '0 0 * * * ?';
String jobID = System.schedule('Update Account', cronExp, sc);