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:
public class ProcessAccountsJob {
{
Id accountId;
String note;
{ .accountId = accountId; .note = note; }
}
List<Payload> items;
Map<Id, Account> accountsById;
{
.items = items;
.accountsById = accountsById;
}
{
List<Account> toUpdate = <Account>();
(Payload p : items) {
accountsById.get(p.accountId);
(a != ) { a.Description = p.note; toUpdate.add(a); }
}
update toUpdate;
}
}