Play Games

Search This Blog

Monday, December 2, 2019

How to schedule a batch job to run only once after 5 mins

How to schedule a batch job to run only once after 5 mins

Assume AccountUpdatorBatchSchedular is the schedular class that schedules the batch job.

global class AccountUpdatorBatchSchedular implements schedulable {
    global void execute(SchedulableContext sc) {
        AccountUpdatorBatch b = new AccountUpdatorBatch();
        database.executebatch(b);
    }
}

AccountUpdatorBatch is the actual batch job.


Sample Code to schedule job:

// Add 5 minutes to current Time
DateTime dtCurrentTime = System.now().addminutes(5);

String sHour = '', sMinute='', sDayOfMonth='', sMonth='', sYear='';

sMinute = String.ValueOf(dtCurrentTime.minute());
sHour = String.ValueOf(dtCurrentTime.hour());
sDayOfMonth = String.ValueOf(dtCurrentTime.day());
sMonth = String.ValueOf(dtCurrentTime.month());
sYear = String.ValueOf(dtCurrentTime.year());   

System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());


Output:



No comments:

Post a Comment