2

This one is a bit odd and I'm hoping it's something someone has seen before. I wrote a batch class that implements both Batchable and Schedulable. I found that I was writing schedulable classes for the sole purpose of scheduling a batch job so why not write a class that implements both and cut out the excess overhead.

I have the job scheduled to run every 15 minutes. So I set up 4 scheduled jobs like so

System.schedule('Entitlements Calculation 00', '0 0 * * * ?', new EntitlementTotalsBatchJob());
System.schedule('Entitlements Calculation 15', '0 15 * * * ?', new EntitlementTotalsBatchJob());
System.schedule('Entitlements Calculation 30', '0 30 * * * ?', new EntitlementTotalsBatchJob());
System.schedule('Entitlements Calculation 45', '0 45 * * * ?', new EntitlementTotalsBatchJob());

It seemed to work fine at first. Meaning when I first scheduled the job, it worked to make the desired changes to historical records.

What I have been finding lately, is that records that should be updated by my batch class, are not being updated. So for simplification purposes, let's assume my batch class simply takes any account that has a certain checkbox checked, and appends '-Checked' to the end of the account name. (I know this is a ridiculous use case, but it's just for explanatory purposes).

So when I first scheduled the jobs, all the accounts that had that box checked, the names indeed had '-Checked' appended. What I found is that moving forward, accounts that were changed to have the box checked, the names were not changing. The batch job is still running, no errors, but no changes to the records.

Here is where it gets weird. If I simply use an Apex Anonymous window and run

Database.ExecuteBatch(new EntitlementTotalsBatchJob(), 100);

The job runs, and the accounts actually update. So I thought it might be an issue with the scheduling of the job. So I scheduled a manual one-time run of the job like below

System.schedule('Entitlements Calculation NOW', '0 06 09 11 03 ? 2015', new EntitlementTotalsBatchJob());

This scheduled the job for a one-time run 3 minutes from when I scheduled it. That caused the accounts to change as well.

So I am completely confused as to why it would work when manually forced,m but when it has been scheduled to run at regular intervals, it's not working in the same manner.

Has anyone seen this before??

Since the job itself seems to work to perform the logic I want, I thought it would be overkill to post the entire code here as its a large class. Here is the basic stricture of the class though

public class EntitlementTotalsBatchJob implements Database.Batchable<SObject>, Schedulable { 

     public final string query;

     public EntitlementTotalsBatchJob() {

          list<Id> accountIDs = EntitlementTotalsBatchUtility.getAccountIDs();

          String idSet = '\'';
          idSet += String.join(accountIDs,'\',\'');
          idSet += '\'';

          query = 'Select Id, Name, OneSign_Support_Level__c, Cortext_Support_Level__c, OneSign_Support_Expiration__c, Cortext_Subscription_End_Date__c, '
          + 'Active_OneSign_Customer__c, OneSign_Customer_Start_Date__c, '
          + 'Active_Cortext_Customer__c, Cortext_Customer_Start_Date__c, '
          + 'Active_Confirm_Id_Customer__c, Confirm_Id_Customer_Start_Date__c, Confirm_Id_Support_Level__c, Confirm_Id_Subscription_End_Date__c '
          + 'From Account Where Id IN (' + idSet + ')';  
     }

     public void execute(SchedulableContext sc) {
          Database.executeBatch(this, 100);
     }

     public Database.QueryLocator start(Database.BatchableContext bc) {
          return Database.getQueryLocator(query);
     }

     public void execute(Database.BatchableContext bc, List<sObject> scope) {
          //Lots of Amazing Logic and Code
     }

     public void finish(Database.BatchableContext bc) {

    }
}

Has anyone seen this type of behavior before?

6
  • Once the schedule has executed does the Apex Jobs page show that the batch size actually contained any records?
    – Mark Keats
    Commented Mar 11, 2015 at 14:39
  • Great question....yes. When the scheduled jobs run its shows 26 batches or around 2,600 accounts. Same thing when I manually use database.ExecuteBatch(), 26 batches also. Just doesn't seem to be executing the logic when scheduled. Commented Mar 11, 2015 at 14:41
  • 1
    My only suggestion would be to try changing the line: public void execute(SchedulableContext sc) { Database.executeBatch(this, 100); } to public void execute(SchedulableContext sc) { Database.executeBatch(new EntitlementTotalsBatchJob(), 100); } I'm just wondering if when the schedule is created there is some kind of caching going on that's causing a strange result when using this. It's a long-shot but maybe worth trying?
    – Mark Keats
    Commented Mar 11, 2015 at 14:52
  • any chance that EntitlementTotalsBatchUtility.getAccountIDs() returns empty list when invoked on the 2nd, 3rd, ... schedule?
    – cropredy
    Commented Mar 12, 2015 at 1:49
  • @Mark Keats, your suggestion is what worked! I really do not know why, perhaps it is a caching issue, but that did the trick. Working perfectly now. If you add it as an answer I will happily up-vote and accept it. Commented Apr 5, 2015 at 2:17

3 Answers 3

3

My only suggestion would be to try changing the line:

public void execute(SchedulableContext sc) { 
  Database.executeBatch(this, 100); 
} 

to

public void execute(SchedulableContext sc) { 
  Database.executeBatch(new EntitlementTotalsBatchJob(), 100); 
}

I'm just wondering if when the schedule is created there is some kind of caching going on that's causing a strange result when using this. It's a long-shot but maybe worth trying?

1
  • I don't pretend to understand why, but this worked. I guess there is some caching going on there, which doesn't seem to make sense, but it worked. Thanks! Commented Apr 7, 2015 at 10:53
0

May be one of solution way add this interface for EntitlementTotalsBatchJob class

implements Database.Stateful

Or you can use

https://www.salesforce.com/us/developer/docs/dbcom_apex290/Content/apex_System_System_scheduleBatch_2.htm

0

Just had similar issue - subclassed instance of scheduler would fail quietly (even when unit tests were fine). Separating scheduler from batch fixed problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .