4

This is my first crack at a scheduled batch Apex class. Copied the code from the manual but missing something. When I run class MassDeleteSchedules in Anonymous block, it works fine. But when I use the UI to schedule it it just hangs in queued status. I gave it a couple hours and nothing changed so cancelled it. There's only 1 batch that's being scheduled.

I feel like the last 2 lines are causing the issue.

global class JobScheduler implements Schedulable{
global void execute(SchedulableContext scMain){
    CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
                      FROM CronTrigger WHERE Id = :scMain.getTriggerId()];

    System.debug(ct.CronExpression);
    System.debug('Number of times batch triggered '+ ct.TimesTriggered);

    //Ensure batch not already triggered
    System.assertEquals(0, ct.TimesTriggered);

    // Sets up query for time period to remove records
    Date dt = Date.today();
    dt = dt.addMonths(-1);
    String dateStr = String.valueOf(dt); // Passing as string for dynamic query below

    String e = 'ONE__Schedule__c';
    String f = 'ONE__Start_Date__c';
    String v = null;
    String q = 'Select id, name from ' + e + ' where ' + f + ' < ' + dateStr;

    MassDeleteRecords del = new MassDeleteRecords(q,e,f,v);       
    Id idbatch = Database.executeBatch(del,2000);
   }
 }
1
  • By design, scheduled Apex jobs will always stays in the Queued status until the job has no NextFireTime. It then gets marked Completed, or if the job is aborted, then the AsyncApexJob row gets marked Aborted. As of the Summer '13 release, this is known/expected behavior: help.salesforce.com/… Jul 23, 2019 at 22:14

3 Answers 3

3

Scheduled Batch Apex jobs appears as Queued status in the Apex Jobs page until the time comes to execute the job (i.e. as per the time you scheduled it to run).

Could it be that you had scheduled it to run by your local clock but the Salesforce time is different and the Job was never actually due to start yet (and hence it will be correctly showing Queued status)?

I see you have put some debug code in the execute() method, did you ever see that in your debug logs? If not, I'd suggest that your Job never even started.

2
  • It does appear the queued problem was related to timezone. I understood that it runs based on users local timezone but when job is submitted it displays PDT, which is confusing. So I was altering the time to account for that. It appears if you submit a scheduled job for a time that has already passed, it sits in queued status...which doesn't seem right. So I'm in EDT and was submitting for 3 hrs earlier.
    – ddeve
    Jul 7, 2013 at 15:26
  • @user2451: maybe your user account is set to the wrong timezone? Verify your SF account is using EDT.
    – Mike Chale
    Dec 9, 2013 at 17:31
0

I've also seen this issue. I'm not sure if normal scheduled jobs do this, but it looks like there is a bug in the apex jobs monitor if the scheduled job is calling a batch job within it. The scheduled job actually runs, but it is just stuck in queue status in the apex jobs monitor.

I know my scheduled job is executing because I see the batch job being kicked off right after it. In addition, I am doing an update to a custom setting within the scheduled job and I see that the setting has actually been updated.

Here is additional information on the issue. There seems to be a workaround to it.

0

When you create a listview on the Apex jobs, Make sure you select Apex class = 'your batch class name'. Note: your schedulded class view always shows either Aborted or Queued

1
  • The primary thing now is that this post is almost 9 years old so lots of times questions like this aren't relevant esp since features of Salesforce didn't exist back then. Occasionally but I think in this case its moot now. I'd focus on more recent posts.
    – ddeve
    Feb 18, 2022 at 19:01

You must log in to answer this question.

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