I have a batch process that I've got scheduled to run after hour (want to increase it to every 15 minutes) and it is currently working 90% of the time just fine, but about once a week is does not get correctly rescheduled for an hour or even sometimes for a full day. There doesn't seem to be a pattern of when it happens and I haven't been able to replicate the issue when testing.

Here is the code:

global class OrderApprovalDeadlineScheduler implements Schedulable {

Boolean cacheLoaded = false;

private BusinessHours getDefaultBusinessHours() {
    BusinessHours bh = new BusinessHours();
    if(cacheLoaded) return bh;
    bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    cacheLoaded = true;
    return bh;

global static String scheduleIt() {
    datetime nextScheduleTime = system.now().addHours(1);
    string minute = string.valueOf(nextScheduleTime.minute());
    string second = string.valueOf(nextScheduleTime.second ());
    string hour = string.valueOf(nextScheduleTime.hour());
    string cronvalue = second + ' ' + minute + ' ' + hour + ' * * ? *';
    string jobName = 'Rescheduling Order Approval Reset Approval Deadline ' + nextScheduleTime.format('hh:mm');

    OrderApprovalDeadlineScheduler scheduler = new OrderApprovalDeadlineScheduler();
    return system.schedule(jobName, cronvalue, scheduler);

global void execute(SchedulableContext sc) {

    BusinessHours bh = getDefaultBusinessHours();
    Map<Id, Order_Approvals__c> recordsToUpdate = new Map<Id, Order_Approvals__c>();
    for(Order_Approvals__c oa : [select Approval_Deadline__c, Approval_Deadlines_Missed__c from Order_Approvals__c where Response__c = NULL]) {
        if(system.now() >= oa.Approval_Deadline__c) {   
            oa.Approval_Deadline__c = BusinessHours.add(bh.Id, oa.Approval_Deadline__c, 32400000);
            oa.Approval_Deadlines_Missed__c = oa.Approval_Deadlines_Missed__c + 1;
            recordsToUpdate.put(oa.Id, oa);
    if(!recordsToUpdate.isEmpty() && recordsToUpdate.size() > 0) {
        update recordsToUpdate.values();



Does anything in this code jump out that could be causing this issue to happen?


| improve this question | | | | |
  • Some debugging ideas (1) replace system.now() with a Util.getNow() method that returns system.now() unless static variable set by Util.setNow() is non null. This allows your testmethods to simulate various values of System.now() versus mocked Order_Approvals__c; (2) Introduce persistent Log__c records that you write on each schedule so you can trace what is happening after-the-fact – cropredy Sep 25 '15 at 17:56

I have encountered the same issue. What's happeining is the chronJob object is locked. I have not found a solution myself, but everytime it encounters the lock error, it automatically set it to run 24 hours later and retry...

Here's the error I see in my org:

First error: Job object is locked and currently processing.

You can view what's happening during each recent run of a job by going to Setup> Apex Jobs

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.