How can you track the execution identifier returned when enqueueing a Queueable Apex job?
Answer
Basics: Queueable jobs run later, so the Id is your only handle. Saving it on a record or log lets you answer "did it finish, and did it fail?"
How it works:
Id jobId = System.enqueueJob(new MyJob(...));- Persist it (a log record, a field on the parent, or a platform event payload).
- Query
AsyncApexJobby Id:Status(Queued, Processing, Completed, Failed, Aborted),NumberOfErrors,ExtendedStatus,CreatedDate,CompletedDate. - Inside the running job,
QueueableContext.getJobId()gives its own Id.
Advanced / deep dive: AsyncApexJob is a sampled, short-lived record, not a durable audit log, so copy key outcomes into your own object. A Finalizer (System.attachFinalizer) runs after the job even on unhandled exception or limit failures and receives a FinalizerContext with getResult() (SUCCESS or UNHANDLED_EXCEPTION), getException() and getAsyncApexJobId(); this is the best place to update the log record to Failed or to enqueue a retry. DML in the same transaction that enqueues the job is not tied to the job; if the transaction rolls back after enqueueJob, the job is also not run. Using AsyncOptions (delay, duplicate signature) is available in newer API versions; verify availability. Remember to bulkify the logging DML.
Code / config example:
Id jobId = System.enqueueJob( (accountIds));
insert (Job_Id__c = jobId, Status__c = , Type__c = );
[
SELECT Status, NumberOfErrors, ExtendedStatus
FROM AsyncApexJob :jobId
];