Concept 5 of 10Asynchronous Apex

Future methods

The original async option: static void methods with primitive parameters, used for callouts and mixed-DML fixes. Still supported, rarely the best choice for new code.

Your confidence:Not rated

Simple explanation

A future method is a static void method annotated with @future. When called, it's queued and runs later in its own transaction with async limits.

public with sharing class UserProvisioning {
    @future
    public static void assignPermissionSets(Set<Id> userIds, String permissionSetName) {
        PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = :permissionSetName LIMIT 1];
        List<PermissionSetAssignment> assignments = new List<PermissionSetAssignment>();
        for (Id userId : userIds) {
            assignments.add(new PermissionSetAssignment(AssigneeId = userId, PermissionSetId = ps.Id));
        }
        Database.insert(assignments, false);
    }
}

Typical uses

  • Mixed DML: insert a contact (non-setup) and then assign a permission set (setup object) in a separate transaction, as above.
  • Callouts from trigger context: @future(callout=true).

Constraints

  • Parameters must be primitives or collections of primitives (no sObjects; pass IDs and re-query).
  • No job ID, no chaining, no guaranteed order between calls.
  • Can't be called from batch or another future method.
  • Up to 50 future calls per transaction.

Real-world analogy

A future method is a sticky note left for the night shift: "please do this later". You can't track it, you can't attach a parcel to it (only short text), and the night shift can't leave sticky notes for itself.

Worked example

A callout that should not block the user, written as a future method, and the Queueable equivalent most teams prefer today:

// Future version
public with sharing class AddressValidation {
    @future(callout=true)
    public static void validate(Set<Id> accountIds) {
        // query accounts, call the address API, update results
    }
}

// Queueable version: job ID, chaining, finalizer, objects as input
public with sharing class AddressValidationJob implements Queueable, Database.AllowsCallouts {
    private final Set<Id> accountIds;
    public AddressValidationJob(Set<Id> accountIds) { this.accountIds = accountIds; }
    public void execute(QueueableContext ctx) {
        // same work; ctx.getJobId() available for logging
    }
}

Diagram

Rendering diagram…

Key points

  • @future = static void, primitive parameters, own transaction.
  • Fixes mixed DML and allows callouts (callout=true).
  • No job ID, no chaining, not callable from batch or future.
  • Pass IDs and re-query inside: data may have changed.
  • Prefer Queueable for new code.

Limits and gotchas

  • Calling a future method in a loop quickly hits the 50-per-transaction limit. Pass a set of IDs once.
  • Future methods called from a trigger run once per trigger chunk; a 10,000-record load produces 50 future calls. Make them idempotent.
  • No order is guaranteed: two future calls can run in either order.
  • In tests, future methods run synchronously at Test.stopTest().

🎀 How to say this in an interview

Be ready to explain why Queueable replaced most future use: job IDs for monitoring, complex parameters, chaining and Finalizers. Mixed DML is still a good example of when async isolation is needed.

Common follow-ups

Why can't future methods take sObjects?

The sObject could change between the time the job is queued and the time it runs, so the platform forces you to pass IDs and re-query current data.

What is mixed DML and how else can you avoid it?

Mixed DML is DML on setup objects (User, Group, PermissionSetAssignment) and non-setup objects in one transaction. Besides @future, use a Queueable, or in tests wrap setup DML in System.runAs.