function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Utkarsh Ugale 8Utkarsh Ugale 8 

How to retrieve Apex Class name from CronTrigger ?

I am working on writing a service which is expected to monitor Scheduled batch jobs. I am querying CronTrigger to get list of all scheduled classes but could not find a direct way to find out which ApexClass is scheduled. 

Is there a way in recent releases to find out which apex class is scheduled in particular CronTrigger record in Apex ?
Ravi Dutt SharmaRavi Dutt Sharma
Hi Utraksh,

Try using below SOQL:
 
select ApexClass.Name, ApexClass.Namespaceprefix, Id, JobItemsProcessed, JobType, Status, NumberOfErrors, MethodName from AsyncApexJob where JobType in ('BatchApexWorker','ScheduledApex')

 
Utkarsh Ugale 8Utkarsh Ugale 8
@Ravi...this query will not work as I want to find an ApexClass name from CronTrigger. Record in AsyncApexJob gets created when scheduled apex is run on given time.
Utkarsh Ugale 8Utkarsh Ugale 8
@niraj CronJobDetail.Name does not give apex class name but name (label) of CronJob record which was entered by user while scheduling an apex. While Scheduling an apex class via 'Schedule Apex' option in setup - > classes, Salesforce asks label & Apex class for scheduling. CronJobDetail.Name returns that label string but not Apex class which was selected.
Raj VakatiRaj Vakati
No direct way with SOQL .. 

You can go to apex scehduerl jobs from the monitering section and you can able to fine the apex class that are using in jobs ..

https://help.salesforce.com/articleView?id=code_apex_job.htm&type=5 

 
Steven McDermottSteven McDermott
This is what I used to get the apex name for the schedulable class for a cron trigger, hope this helps someone in the future looking for this
 
List<CronTrigger> ct = [Select Id, CronJobDetail.Name FROM CronTrigger];



List<AsyncApexJob> aj = [SELECT ApexClass.Name,CronTriggerId from AsyncApexJob
where JobType in ('ScheduledApex') ];



for (CronTrigger c : ct) {
    for (AsyncApexJob a : aj) {
        if (c.Id == a.CronTriggerId) {
           System.debug(c.CronJobDetail.Name + ' = ' + a.ApexClass.Name);
           break;
         }
     }
}