# How can developers programmatically monitor and reorder jobs sitting in the Asynchronous Apex Flex Queue?

**Topic:** Apex · **Difficulty:** Medium · **Tags:** Apex, Asynchronous Apex, Flex Queue

Read it on the site: https://www.forcechallenges.com/interview-questions/how-can-developers-programmatically-monitor-and-reorder-jobs-sitting-in-the-asynchronous-apex-flex-queue

## Answer

**Basics:** Jobs waiting in the Flex Queue are processed in order. If an urgent job is stuck behind a big one, you can move it forward without aborting anything.

**How it works:**
1. Find Holding jobs: `SELECT Id, ApexClass.Name FROM AsyncApexJob WHERE Status = 'Holding'`.
2. Call `FlexQueue.moveJobToFront(jobId)`, `moveBeforeJob(jobToMove, jobInQueue)`, `moveAfterJob(jobToMove, jobInQueue)` or `moveJobToEnd(jobId)`.
3. Each returns a Boolean for success.
4. The platform starts jobs from the front as slots free.

**Advanced / deep dive:** Only jobs with status Holding can be moved. The `FlexQueueItem` object exposes `AsyncApexJobId`, `JobType` and `JobPosition` for reporting. The Apex Flex Queue page in Setup offers drag and drop. For automated prioritization, a monitoring scheduled job can scan the queue and move jobs of a critical class to the front. Be careful, because constantly promoting jobs starves others. In tests, `Test.enqueueBatchJobs(n)` fills the Flex Queue with dummy jobs and `Test.getFlexQueueOrder()` returns the order, letting you verify reordering logic.

**Code / config example:**
```apex
// Promote the nightly integration batch to the front of the queue
List<AsyncApexJob> held = [
    SELECT Id FROM AsyncApexJob
    WHERE Status = 'Holding' AND ApexClass.Name = 'NightlyIntegrationBatch'
    LIMIT 1
];
if (!held.isEmpty()) {
    Boolean ok = FlexQueue.moveJobToFront(held[0].Id);
    System.debug('Moved: ' + ok);
}
```

**Hidden gotchas & must-remember points:**
- `System.minFlexQueue`, `maxFlexQueue` and `updateFlexQueue` are not real methods.
- Only Holding jobs are movable; Queued or Processing are not.
- Maximum 100 Holding jobs.
- Default order is first-in, first-out.
- Reordering needs suitable admin permissions.
- Scheduled batch jobs that cannot start also land in the Flex Queue.

**Best practice:** Keep critical jobs few and short, and use reordering as an exception, not the standard design.

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