I have been implementing Dan Appleman's solutions since his appearances at Dreamforce, and am currently working with his Going Asynchronous solution and running into a snag. I put a partial solution in place, but have (not infrequently) run into the problem of trying to kick off a batch when there are already too many (5) batches running.

My eclipse version is Kepler, with the most recent Force.com IDE (API version 30.0, at any rate).

In anonymous apex I can write:

List<CronTrigger> jobs = [Select Id, CronJobDetail.Name, State, NextFireTime From CronTrigger limit 10];
// This part is just for reference purposes, clearly, and I am getting results back
for(CronTrigger job : jobs) {

without a problem (from his StartScheduler() method, Chapter 7). When I try to put the same query into my class, however, I get the following error:

Save error: No such relation 'CronJobDetail' on entity 'CronTrigger'. If you are attempting <blah blah blah>...

The remaining code, which should tell me if the job has already been scheduled, is unavailable because the query is disallowed. Here it is for reference:

        if(jobs.size() > 0 && jobs[0].state != 'COMPLETED' && jobs[0].state != 'ERROR' && jobs[0].state != 'DELETED') {
        // The job's already running; is the batch running?
        Set<String> activejobstates = 
          new Set<String> {'Queued', 'Processing', 'Preparing'};

        List<AsyncApexJob> apexjobs = 
          [Select Id, ApexClass.Name
           from   AsyncApexJob
           where  ApexClass.Name = 'AsyncProcessor'
           and    Status in :activejobstates];

        // If the batch is running, quit
        if(apexjobs.size() > 0) return;

        // If it's about to run, quit
        if(DateTime.Now().AddSeconds(60) > jobs[0].NextFireTime) return;    

[I (still) find it odd that the code works anonymously, but not saved to a class.]

Has anyone else run into this problem and/or figured out a solution? Is it an API issue, that I need to back down to previous version to get this through? I need to figure it out, as I have some scheduled jobs that are manually scheduled, and other scheduled jobs that use a truncated version of this ^ process, and they're colliding.

  • 3
    You should double check what version your class is when you are saving. Maybe try it in Dev Console first. I suspect it is an API version thing. – Daniel Hoechst Aug 5 '14 at 14:10
  • You are correct, sir! Excellent. (It would've been more excellent had I figured this out sooner, but hey...). Thanks! – Duncan Stewart Aug 5 '14 at 14:41

You couldn't access the Job Name until Winter '14 so I suspect it is due to an API version. Double check the API version of the class being saved from Eclipse. You can also use Developer Console to update the API version or make changes to test there.

| improve this answer | |
  • I hadn't realized Dan's book was quite so current. I'll need to keep a closer eye on my versions, I guess. Thanks, Daniel! – Duncan Stewart Aug 5 '14 at 14:46

This query will work.

SELECT CreatedById,CreatedDate,CronExpression,CronJobDetailId,EndTime,Id,LastModifiedById,NextFireTime,OwnerId,PreviousFireTime,StartTime,State,TimesTriggered,CronJobDetail.name FROM CronTrigger

| improve this answer | |
  • 1
    Not for all api versions. – martin Nov 9 '15 at 8:47

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.