Salesforce - Developer - Monitoring Asynchronous Jobs


// Salesforce - Developer - Monitoring Asynchronous Jobs:

We can monitor the status of all jobs in the Salesforce user interface by:

1. Go to Setup
2. Click on "Jobs | Apex Jobs".

We can also monitor the status of Apex jobs in the Apex Flex Queue, and reorder 
them to control which jobs are processed first. You can get to this queue by 
going to Setup and clicking Jobs | Apex Flex Queue.

Future jobs show up on the Apex Jobs page like any other jobs. However, future 
jobs are not part of the flex queue at this time.

We can query AsyncApexJob to find your future job, but there’s a caveat. Since 
kicking off a future job does not return an ID, you’ll have to filter on some 
other field like MethodName, JobType, etc. to find your job. There are a few 
sample SOQL queries in this Stack Exchange post that may help.

To query information about your submitted job, perform a SOQL query on 
AsyncApexJob by filtering on the job ID that the System.enqueueJob method 

AsyncApexJob jobInfo = [SELECT Status, NumberOfErrors
    FROM AsyncApexJob WHERE Id = :jobID];

The Apex Flex queue enables you to submit up to 100 batch jobs for execution. 
Any jobs that are submitted for execution are in holding status and are placed 
in the Apex Flex queue. Up to 100 batch jobs can be in the holding status.

Jobs are processed first-in first-out—in the order in which they’re submitted. 
You can look at the current queue order and shuffle the queue, so that you 
could move an important job to the front, or less important ones to the back.

When system resources become available, the system picks up the next job from 
the top of the Apex Flex queue and moves it to the batch job queue. The system 
can process up to five queued or active jobs simultaneously for each 
organization. The status of these moved jobs changes from Holding to Queued. 
Queued jobs get executed when the system is ready to process new jobs. Like 
other jobs, you can monitor queued jobs in the Apex Jobs page.

After an Apex job has been scheduled, you can obtain more information about it 
by running a SOQL query on CronTrigger. The following sample queries the number 
of times the job has run, and the date and time when the job is scheduled to 
run again. It uses a jobID variable which is returned from the System.schedule 

CronTrigger ct = [SELECT TimesTriggered, NextFireTime FROM CronTrigger 
  WHERE Id = :jobID];

If you’re performing this query inside the execute method of your schedulable 
class, you can obtain the ID of the current job by calling getTriggerId on the 
SchedulableContext argument variable.

global class DoAwesomeStuff implements Schedulable {
    global void execute(SchedulableContext sc) {
        // some awesome code
        CronTrigger ct = [SELECT TimesTriggered, NextFireTime FROM 
            CronTrigger WHERE Id = :sc.getTriggerId()];

You can also get the job’s name and the job’s type from the CronJobDetail 
record associated with the CronTrigger record. To do so, use the CronJobDetail 
relationship when performing a query on CronTrigger. This example retrieves the 
most recent CronTrigger record with the job name and type from CronJobDetail.

CronTrigger job = [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, 
  CronJobDetail.JobType FROM CronTrigger ORDER BY CreatedDate DESC LIMIT 1];

Alternatively, you can query CronJobDetail directly to get the job’s name and 
type. The following example gets the job’s name and type for the CronTrigger 
record queried in the previous example. The corresponding CronJobDetail record 
ID is obtained by the CronJobDetail.Id expression on the CronTrigger record.

CronJobDetail ctd = [SELECT Id, Name, JobType FROM CronJobDetail 
  WHERE Id = :job.CronJobDetail.Id];

And lastly, to obtain the total count of all Apex scheduled jobs, excluding all 
other scheduled job types, perform the following query. Note the value '7' is 
specified for the job type, which corresponds to the scheduled Apex job type. 
See CronJobDetail in the Resources section below for a list of all types.

SELECT COUNT() FROM CronTrigger WHERE CronJobDetail.JobType = '7’
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License