# How can you pass non-primitive collections into Queueable Apex jobs for processing?

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

Read it on the site: https://www.forcechallenges.com/interview-questions/how-can-you-pass-non-primitive-collections-into-queueable-apex-jobs-for-processing

## Answer

**Basics:** Unlike `@future`, which allows only primitives or collections of primitives, a Queueable is a normal Apex object. Anything serialisable can be a field: SObjects, `List<Account>`, `Map<Id, List<Contact>>`, enums, and your own wrapper classes. There is no special "Iterable" mechanism; it is simply object state.

**How it works:** Define a class with private fields and a constructor. When you call `System.enqueueJob(new MyJob(data))`, Salesforce serialises the instance, including its fields. At execution time, a fresh instance is rebuilt with those values and `execute` runs. The data is a point-in-time snapshot.

**Advanced / deep dive:** Prefer passing Ids or small DTOs and re-querying in `execute` when freshness matters, because records may have changed while the job waited, and because serialised state counts against heap and limits queue payload. When you do pass SObjects, pass only the fields you need. Custom classes must have serialisable members; mark non-serialisable ones `transient` and rebuild them in `execute`. Inner classes work but top-level or static inner classes are safest; changing the class shape while jobs are queued can break deserialisation after a deployment, so deploy during quiet periods. For chained jobs, pass progress state (for example an offset or remaining list) to the next job. Large collections should be split: process N per job and chain the remainder, keeping each execution inside the async heap (12 MB) and CPU (60,000 ms) limits. Queueable callouts are allowed directly, and you can attach a Finalizer for failure handling.

**Code / config example:**
```apex
public class ProcessAccountsJob implements Queueable {
    public class Payload {
        public Id accountId;
        public String note;
        public Payload(Id accountId, String note) { this.accountId = accountId; this.note = note; }
    }
    private List<Payload> items;                 // custom class collection
    private Map<Id, Account> accountsById;       // SObject map

    public ProcessAccountsJob(List<Payload> items, Map<Id, Account> accountsById) {
        this.items = items;
        this.accountsById = accountsById;
    }
    public void execute(QueueableContext ctx) {
        List<Account> toUpdate = new List<Account>();
        for (Payload p : items) {
            Account a = accountsById.get(p.accountId);
            if (a != null) { a.Description = p.note; toUpdate.add(a); }
        }
        update toUpdate;
    }
}
```

**Hidden gotchas & must-remember points:**
- Serialised data is a snapshot and may be stale; re-query when correctness matters.
- Serialised state counts against heap; keep payloads lean.
- Non-serialisable types (for example some system or Http objects) fail at enqueue time.
- Changing the class definition between enqueue and run can break queued jobs.
- Static variables are not carried over; only instance fields are.
- Only primitives and collections of primitives are allowed in `@future`; this is a key reason to choose Queueable.

**Best practice:** Pass the minimum data needed (Ids plus small DTOs) and re-query the rest inside `execute`.

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