I created a basic batch class.
global class MyBatchClass implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id, Name FROM Account');
}
global void execute(Database.BatchableContext bc, List<Account> scope) {
for(Account acc : scope) {
acc.Name = 'Updated-' + acc.Name;
}
update scope;
}
global void finish(Database.BatchableContext bc){
System.debug('Batch job completed.');
}
}
Below is the schedulable class
global class MyBatchClassSchedule implements Schedulable{
global void execute(SchedulableContext sc) {
MyBatchClass batchJob = new MyBatchClass();
Database.executeBatch(batchJob);
}
}
Now, I am trying to schedule this batch class using the following CRON expression
String hour = String.valueOf(Datetime.now().hour());
String min =String.valueOf(Datetime.now().addMinutes(5).minute());
String ss = String.valueOf(Datetime.now().second());
String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';
String jobName = 'MyBatchSchedulerJob'; String cronExpression = '0 */5 * * * ?';
System.schedule(jobName, cronExpression, new MyBatchClassSchedule());
It is scheduling the class for the first 5 minutes. eg If it starts at 12:00AM, then the next scheduled time comes 12:05. But when it starts again at 12:05, the next scheduled time is 12:05 instead of 12:10. What am I doing wrong?
global
access modifier unless you need the class to be visible across package namespace boundaries (which is quite unlikely).