I have a set of records(around 90,000 records) to be updated at 1AM every day. But I cannot update at least 10,000 records in a bulk as salesforce reaches governor limits. Then I wrote a schedulable class as follows.
global class ScheduledCustomObjectUpdater implements Schedulable {
global void execute(SchedulableContext SC) {
List<Custom_Object__c> customObjects = [SELECT Name
WHERE SystemModStamp < :Date.today()
LIMIT 5000];
updateCustomObjects(customObjects);
finish(SC, customObjects);
}
private void updateCustomObjects(List<Custom_Object__c> customObjects) {
List<Custom_Object__c> customObjectsToUpdate = new List<Custom_Object__c>();
for(Custom_Object__c customObject : customObjects) {
customObject.Name = 'some text here';
}
update customObjectsToUpdate;
}
private void finish(SchedulableContext SC, List<Custom_Object__c> customObjects) {
CronTrigger job = [SELECT CronJobDetail.Name FROM CronTrigger WHERE Id = :SC.getTriggerId()];
System.abortJob(job.Id);
if(customObjects.size() == 5000) {
Datetime next = DateTime.now().addMinutes(1);
String hour = String.valueOf(next.hour());
String min = String.valueOf(next.minute());
String ss = String.valueOf(next.second());
//parse to cron expression
String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ? *';
// schedule a new job in next minute
System.schedule(job.CronJobDetail.Name, nextFireTime, new ScheduledCustomObjectUpdater());
} else {
// all done. schedule a job for tomorrow
System.schedule(job.CronJobDetail.Name, '0 0 1 * * ? *', new ScheduledCustomObjectUpdater());
}
}
}
Then I scheduled this class as a job on my sandbox
at some time during the day(yesterday) using Dev Console Annonymous Window
. it updated all the records and sheduled a new job at 1AM following day(today).
But when I check the records today, they have been updated at 10:00AM. My expectation was to update the records at 1AM.
Hoping any help to figure out what's going on there.