# How can you track the execution identifier returned when enqueueing a Queueable Apex job?

**Topic:** Apex · **Difficulty:** Medium · **Tags:** Apex, Asynchronous Apex, Queueable Apex

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. `Id jobId = System.enqueueJob(new MyJob(...));`
2. Persist it (a log record, a field on the parent, or a platform event payload).
3. Query `AsyncApexJob` by Id: `Status` (Queued, Processing, Completed, Failed, Aborted), `NumberOfErrors`, `ExtendedStatus`, `CreatedDate`, `CompletedDate`.
4. 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:**
```apex
Id jobId = System.enqueueJob(new SyncAccountsJob(accountIds));
insert new Job_Log__c(Job_Id__c = jobId, Status__c = 'Queued', Type__c = 'AccountSync');

// Later, in a monitor
AsyncApexJob j = [
    SELECT Status, NumberOfErrors, ExtendedStatus
    FROM AsyncApexJob WHERE Id = :jobId
];
```

**Hidden gotchas & must-remember points:**
- The returned Id is an AsyncApexJob Id, not a CronTrigger Id.
- If the enclosing transaction rolls back, the job does not start.
- AsyncApexJob rows are purged after some days, so store outcomes yourself.
- Finalizers are the only reliable way to catch limit-exception failures.
- Only 50 jobs can be enqueued per sync transaction; 1 from within a queueable.
- Test context returns an Id but the job runs only at `stopTest()`.

**Best practice:** Log the Id at enqueue time and update the log from a Finalizer.

---
Source: ForceChallenges · last updated 2026-11-02
