Sub topic 4
Enqueuing a Job and Tracking It with AsyncApexJob
This chapter assumes you can write a basic Queueable class, run SOQL, and know how governor limits work within a transaction. Here you will enqueue a job, capture its ID, and check its status.
What enqueuing does
System.enqueueJob takes an instance of a class that implements Queueable. It does not run the job. It places the job in the queue and returns the ID of the job's AsyncApexJob record. The job runs later in its own transaction, when system resources allow. Don't assume it starts right away or that jobs run in the order you enqueued them.
Id jobId = System.enqueueJob(new AccountRatingJob(accountIds));
System.debug('Enqueued job: ' + jobId);Keep the returned ID if you need to track or cancel the job later. You can store it on a record, return it to a caller, or log it.
Tracking a job with AsyncApexJob
AsyncApexJob is a standard object. Each queued, running, or finished async job has one record. Query it by the ID you got back:
AsyncApexJob job = [
SELECT Id, Status, JobType, NumberOfErrors, ExtendedStatus,
CreatedDate, CompletedDate, ApexClass.Name
FROM AsyncApexJob
WHERE Id = :jobId
];
System.debug(job.ApexClass.Name + ' is ' + job.Status);These fields are the most useful ones for queueables:
| Field | Meaning |
|---|---|
| `Status` | Where the job is in its life cycle (see below). |
| `JobType` | `Queueable` for these jobs. Filter on it when you query broadly. |
| `NumberOfErrors` | Count of errors recorded for the job. |
| `ExtendedStatus` | Error text when the job failed. |
| `ApexClass.Name` | The class that was enqueued. |
| `CompletedDate` | Set when the job finishes. |
Job status life cycle
A queueable job moves through a small set of states.
Completed means the execute method returned normally. Failed means an unhandled exception ended the job. The same record also appears in Setup > Apex Jobs, where you can watch jobs without writing a query.
Cancelling a queued job
System.abortJob(jobId) stops a job that has not started yet. It moves the record to Aborted.
System.abortJob(jobId);Limits when enqueuing
Enqueuing counts against limits in the transaction that does it:
- A synchronous transaction can enqueue up to 50 jobs.
- A running queueable can enqueue only one child job. Chaining is covered in a later chapter.
Check how much room is left with the Limits class:
System.debug(Limits.getQueueableJobs() + ' of '
+ Limits.getLimitQueueableJobs() + ' queueable jobs used');Enqueuing inside a loop is a common bug. Pass a whole list to one job instead of one job per record.
Common mistakes
- Querying right after enqueuing and expecting
Completed. The job is almost alwaysQueuedat that point. Poll later, or use a finalizer (covered later) to react when the job ends. - Ignoring the return value. Without the ID, you cannot find the exact job record afterward.
- Reading
ExtendedStatusfor successful jobs. It is only populated when something went wrong.
Exercise
You enqueue a queueable job from a trigger and immediately query AsyncApexJob by the returned ID. The status is not Completed. Is that a bug, and which two statuses could you expect to see?
Show answer
It is not a bug. Enqueuing only queues the job, and it runs later in a separate transaction. Right after enqueuing you will normally see Queued, and Processing if it has already started.