Linking CronTrigger sObjects to Scheduled Jobs listings

There doesn't currently appear to be any mechanism for determining the apex class or the name of the scheduled job from Apex or via the API (See Ideas:CronTrigger Name field needs to be exposed).

As a stop gap measure, you could create a custom object or setting and manually map from the CronTriggerId returned by System.schedule() and the data that you require (ApexClass name or Id). Reference - Need a way to programatically change or delete a Scheduled Job


Rich Van Dev posted a comment on an idea in the IdeaExchange that contained a really nice hack/work-around:

There is a way to do this though it is undocumented and could be subject to breaking with new SF releases.

It is possible to add text after the year element of the Cron Expression - this text is ignored when the cron expression is parsed however it is available when querying the CronTrigger by CronExpression with a filter like "where CronExpression LIKE '% NAMEOFMYSCHEDULEDJOB'".

Example:

  System.schedule('My Job Name', '0 59 23 31 12 ? 2013 MyJobName', new MyJob());

  List<CronTrigger> listCronTrigger  = [
      SELECT Id, OwnerId, CronExpression 
      FROM CronTrigger 
      WHERE CronExpression like '% MyJobName'];
  system.debug(listCronTrigger);

Also, the idea is scheduled for release in Winter 2014.


I use the following thing to match the CronTrigger with AsyncApexJob object which has information about Schedulable class which is scheduled.

I join these two tables by CreatedDate field.

It appears if these jobs are scheduled by a user, it can 100% match the CronTrigger with AsyncApexJob object and through it retrieve the name of Schedulable class which is scheduled.

However, if I schedule it from code when several scheduled jobs are being scheduled in the same second, this doesn't work. So I could just wait a second between scheduling jobs but it will hit 10000 milliseconds Apex CPU Time Limit if I had more than 9 Schedulable Jobs.

Here is my code snippet to backup scheduled jobs https://docs.google.com/document/d/1Rj2CFnPcFs3xC82N2snX1j21k9TlIi3325MQmA0R8qg and here to restore scheduled jobs from backup (user should change in the first line name of the debug object which contains the backup) https://docs.google.com/document/d/162ChaB_6Lkgp89bHhbKZjp7tHQPpYODC5gQnX9vxI_I

Also there is here debug log object xml which I use to put backup in JSON format inside one of its fields. https://docs.google.com/document/d/1UoM64UsTzIPKkTQdfaQ0D8k85aSL_o6BlwJOW-lbW1g

Tags:

Apex

Scheduled