Sub topic 12
Monitoring Future Jobs in Setup
This chapter assumes you can already declare and call a @future method, and that you know SOQL and the basics of governor limits. Future methods run after your transaction ends, so you can't see their results in the code that called them. Setup and the AsyncApexJob object show you whether they ran and whether they failed.
Finding future jobs in Setup
Each call to a future method creates one job record. To view them:
- Open Setup.
- Type "Apex Jobs" in Quick Find and select Apex Jobs. In Lightning Experience this page is under Environments > Jobs.
- Find rows where Job Type is
Future Method.
The columns most useful for future jobs are:
| Column | What it tells you |
|---|---|
| Submitted Date | When the call was queued |
| Status | Where the job is in its life cycle |
| Status Detail | The error message if the job failed |
| Method Name | The future method that ran |
| Apex Class | The class that contains the method |
| Submitted By | The user whose transaction made the call |
| Completed Date | When the job finished |
Future jobs have no batches, so the batch-related columns, such as Total Batches, don't tell you anything for them.
Job life cycle
A future job moves through a few statuses. It is queued when your transaction calls the method. It becomes Processing when Salesforce starts it, and it ends as Completed or Failed.
A job can sit in Queued for some time. Salesforce runs future methods when resources are available, so queue time is normal and isn't a sign of an error.
Reading failures
A future method that throws an unhandled exception ends as Failed. The exception message appears in Status Detail. The failure doesn't roll back or affect the transaction that called the method, because that transaction finished earlier.
Failed jobs are not retried. If the work matters, catch exceptions inside the method and record them somewhere you can find them, such as a custom log object. Otherwise you have to find the failure in the Apex Jobs page.
For more detail, open Debug Logs in Setup and add a trace flag for the user in the Submitted By column. The future method's execution gets its own log, separate from the log of the transaction that called it.
Querying jobs with SOQL
Future jobs are stored in the AsyncApexJob object, so you can query them from code or from the Developer Console. This snippet lists the most recent future jobs from today:
List<AsyncApexJob> jobs = [
SELECT Id, ApexClass.Name, MethodName, Status, ExtendedStatus,
NumberOfErrors, CreatedDate, CompletedDate
FROM AsyncApexJob
WHERE JobType = 'Future' AND CreatedDate = TODAY
ORDER BY CreatedDate DESC
LIMIT 20
];
for (AsyncApexJob job : jobs) {
System.debug(job.ApexClass.Name + '.' + job.MethodName + ' -> ' + job.Status
+ (job.ExtendedStatus != null ? ' (' + job.ExtendedStatus + ')' : ''));
}To see only problems, add AND Status = 'Failed' to the WHERE clause. ExtendedStatus holds the same error text that Status Detail shows.
Common errors when monitoring
- Looking for a job ID in the calling code. A future method returns
void, so the call gives you no job ID. To find a job, filterAsyncApexJobby class, method name, and time. If you need an ID to track a job, use Queueable Apex, whereSystem.enqueueJobreturns one. - Expecting the job to exist in a test. Jobs queued in a test run when
Test.stopTest()is called, and test data is rolled back afterward. Assert on the data the method changed instead of on job records. - Missing a limit problem. Future calls count against a daily limit for asynchronous Apex executions, shared with other async types. If many jobs appear and some are never created, check your call volume before assuming the code is wrong.
- Treating Completed as success of the business logic. A job whose method catches its own exceptions still shows Completed. Check your own log, or verify the data.
Exercises
Exercise
A future method throws an unhandled exception. What status does its job show in Apex Jobs, where do you find the error message, and does the failure undo the changes made by the transaction that called it?
Show answer
The job shows Failed. The error message is in Status Detail (ExtendedStatus in SOQL). The calling transaction is not affected, because it had already finished before the future method started.
Exercise
Write a SOQL query that returns the class name, method name, and error text of all failed future jobs, newest first.
Show answer
SELECT ApexClass.Name, MethodName, ExtendedStatus FROM AsyncApexJob WHERE JobType = 'Future' AND Status = 'Failed' ORDER BY CreatedDate DESC