21

I am trying to get the name of the schedule / batch classes called by the schedule job

Trying this

select Id,CronJobDetailId,CronJobDetail.Name,
       NextFireTime,PreviousFireTime,State,StartTime,EndTime,CronExpression,
       TimeZoneSidKey,OwnerId,TimesTriggered 
from CronTrigger 
where CronJobDetail.Name = 'Assetupdates'

The job was scheduled via execute anonymous and given a very undescriptive name

Thanks

5 Answers 5

28

Ok, found what I was looking for

select ApexClassId, Id, JobItemsProcessed, JobType, 
       Status, NumberOfErrors, MethodName 
from AsyncApexJob  
where JobType in ('BatchApexWorker','ScheduledApex')
3
  • 3
    I'm having a bit of trouble with this myself. Not sure if my understanding is correct, but I believe querying "AsyncApexJob" will only give details on a job that has already been executed/entered the queue. Is there any way to get the class name from a scheduled job that has not been executed yet? Annoyingly, there doesn't seem to be a class ID or class name field in CronJobDetail... Commented Mar 21, 2017 at 10:49
  • 2
    AsyncApexJob also has entries for Scheduled Jobs which are not fired yet. They will be in 'Queued' status. As you said, based on current datamodel it is not possible correlate Job Name to Apex Class name.
    – metasync
    Commented Nov 2, 2017 at 23:35
  • 1
    I tested the following query (showing the class name and namespace instead of class id): select ApexClass.Name, ApexClass.Namespaceprefix, Id, JobItemsProcessed, JobType, Status, NumberOfErrors, MethodName from AsyncApexJob where JobType in ('BatchApexWorker','ScheduledApex') Commented Aug 9, 2018 at 10:11
6

Thank you Scott, you can also use the field ApexClass.name in your query:

SELECT ApexClassId,
       ApexClass.name,
       Id,
       JobItemsProcessed,
       JobType,
       Status,
       NumberOfErrors,
       MethodName
FROM AsyncApexJob
WHERE JobType in ('BatchApexWorker',
                  'ScheduledApex')
5

Unfortunately there isn't any direct link between the CronTrigger table and the related apex class id (at least that they expose). See the these two ideas to vote it up. That said you've got some options.

Option 1 - Setup Menu

Sometimes you can get it from the setup. Go to setup->monitor->jobs->scheduled jobs, and you'll see a list of all scheduled jobs. For apex classes that were scheduled through the 'Schedule Apex' button in setup->develop->apex classes, you'll see a 'Manage' link next to the job. Click that and you'll see the class name associated with that job.

Option 2 - Test Delete Schedulable Classes

However, it turns out the metadata api is kind enough to return the id of the CronTrigger records associated with an apex class if you try and delete it.

First you'll need to get a list of all schedulable apex classes then you org and then try and delete them with the metadata api. If say there was just one, MySchedulableClass you would you would have the following destructiveChanges.xml

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>MySchedulableClass</members>
        <name>ApexClass</name>
    </types>
    <version>41.0</version>
</Package>

If you do a test deployment, you'll get an error like the following which includes the crontrigger ids (they start with 08e)

BUILD FAILED
/Users/yourname/ant/sfse/build.xml:95: The following error occurred while executing this line:
/Users/yourname/ant/sfse/build.xml:55: 
*********** DEPLOYMENT FAILED ***********
Request ID: 0AfXXXXXXXXXXXXXXX

All Component Failures:
1.  classes/MySchedulableClass.cls -- Error: This apex class is referenced elsewhere in salesforce.com.  Remove the usage and try again. : Scheduled Jobs - 08eXXXXXXXXXXXX. This Schedulable class is in use. : Apex Class.

*********** DEPLOYMENT FAILED ***********

Then it's just a matter of searching those errors for the id of the particular CronTrigger you're interested in and you'll have your answer.

CAVEAT I expect this approach won't work for schedulable classes that are part of a managed package since you won't be able to delete those with the metadata api, but who knows, worth trying.

Option 3 - Correlate with AsyncApexJob Table

Based on @michael's answer. In most cases schedulable jobs will start a batch job. If that's the case you can look at the last fired date for the scheduled jobs list and try and find the corresponding job in the AsyncApexJob table based on the created date for the AsyncApexJob, then you can inspect the ApexClassId column.

1
  • @michael_gill, this is really what you were looking for right? Commented Nov 3, 2018 at 8:43
4
List<AsyncApexJob> listfetched = [select ApexClass.Name, Id, JobItemsProcessed, JobType, Status, NumberOfErrors, MethodName , CronTrigger.CronJobDetail.Name from AsyncApexJob where JobType in ('BatchApexWorker', 'ScheduledApex')];
for(AsyncApexJob a : listfetched)
{
    System.debug(a.ApexClass.Name +' >>> '+a.CronTrigger.CronJobDetail.Name);
}
2

Since API v53.0 you can do the following to get the Apex class name related to a BatchApex and ScheduledApex job :

SELECT Id, ApexClass.Name, MethodName, JobType, Status, LastProcessed, JobItemsProcessed, NumberOfErrors
FROM AsyncApexJob
WHERE CronTrigger.CronJobDetail.Name = 'Name of your scheduled/batch job'
AND JobType in ('BatchApexWorker', 'ScheduledApex')

Be aware that it does not work through api (with navigator extensions for example) only in dev console or apex code.

You must log in to answer this question.

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