+ Start a Discussion
zachelrathzachelrath 

How to Query JUST Scheduled APEX Jobs (i.e. ignore Report/Dashboard Refreshes)

Just to set the context, my use case is: programmatically scheduling Scheduled Apex Jobs from other pieces of Apex code.

 

I know that there is a limit on the number of Scheduled APEX Jobs (i.e. scheduled executions of Apex classes implementing the Schedulable interface), but I'm having trouble figuring out a way to distinguish between APEX Scheduled Jobs and all other Scheduled Jobs (i.e. Scheduled Report/Dashboard Refreshes) using a query on the CronTrigger object. Right now I have the following:

 

public static final Integer MAX_SCHEDULED_APEX_JOBS = 10; 
	
// Determine whether the maximum number of Scheduled APEX Jobs has been reached
public static Boolean MaxScheduledJobsReached() {
   return (GetScheduledJobs().size() >= MAX_SCHEDULED_APEX_JOBS) ;
}
	
// Returns all Scheduled Apex jobs that have not been started yet 
public static List<CronTrigger> GetScheduledJobs() {
   return [select Id, NextFireTime 
	   from CronTrigger 
           where State in ('WAITING','ACQUIRED','EXECUTING')
            or NextFireTime != NULL];
}	

The problem is that this returns Dashboard and Report Refreshes as well. How do I weed-out Report and Dashboard Refresh Jobs from this query?

 

I thought of adopting a naming convention for Jobs, i.e. whenever I programmatically Schedule an Apex Job, I name it something like 'ApexJob__' etc, but if there were other Scheduled Apex Jobs scheduled that I didn't create, this wouldn't work.

 

Looking at the Scheduled Jobs page in the UI (i.e. Administration Setup > Monitoring > Scheduled Jobs), there's a Type field displayed that is not accessible through the CronTrigger API object. If only it was!!! This would be super helpful.

 

Any suggestions???

 

Thanks!

Zach McElrath

Skoodat LLC

 

 

 

hisrinuhisrinu

Hello Zach,

 

Did you ever get answer for this question? If yes can you please share

 

 

zachelrathzachelrath

Hi Srini,

 

The easiest way to do it is to use a Custom Setting/Object as a "man in the middle" to keep track of the Ids, Names, and any custom information desire of all Apex jobs you Schedule. That way you can quickly query this table to get the Ids, and use that to do System.abortJob(), etc., or to query the CronTrigger table for NextFireTime and whatever other fields you're interested in.

 

I tried querying CronTrigger based on various combinations of the State and NextFireTime fields, but that proved unreliable.

 

Regards,

 

Zach

hisrinuhisrinu

It does not really work for us as we have too many admins :)

 

Appreciate your quick response, Thank You !!!