How to Query All the batch classes in your Salesforce Instance using APEX

You can use the following scripts to know the batch classes in your Salesforce instance.


List asyncApexJobList = [select ApexClassId, Id, JobItemsProcessed, JobType, Status, 
NumberOfErrors, MethodName from AsyncApexJob  where 
JobType in ('BatchApexWorker','BatchApex')];

List idList = new List();
for(AsyncApexJob eachRecord:asyncApexJobList){
    idList.add(eachRecord.ApexClassId);
}

List apexClassList = [Select Name from ApexClass where Id IN:idList];
for(ApexClass eachApexClass : apexClassList){
    System.debug(eachApexClass.Name);
}

Additionally, You can use above to get all schedulable and other classes by adding the JobType filter. The possible values for JobType are:

  1. Future
  2. SharingRecalculation
  3. ScheduledApex
  4. BatchApex
  5. BatchApexWorker
  6. TestRequest
  7. TestWorker
  8. ApexToken
  9. Queueable
Also, you can use the above script to find the status of the job as well by adding a filter on AsyncApexJob.Type. The possible values are:

  1. Holding
  2. Queued
  3. Preparing
  4. Processing
  5. Aborted
  6. Completed
  7. Failed

Please refer below URL for details regarding AsyncApexJob.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_asyncapexjob.htm

Comments

Popular posts from this blog

System.AsyncException: The Apex job named is already scheduled for execution.

Custom Setting Creation in Apex Test Class

Error : "Trigger must be associated with a job detail"

AJAX Toolkit Debug Shell

Salesforce Trace Log Query Using Apex

how to get object name from id / prefix value in salesforce

Creating Salesforce Developer Account

Creating MAP from SOQL Query

Custom Pagination Without Controller (Apex)