Async APEX - astromechanic/cheat_sheets GitHub Wiki

Future Apex

public class SomeClass {
  @future
  public static void someFutureMethod(List<Id> recordIds) {
    ...
  }
}

Future apex is always static and always returns void. They get only primitive data types as parameter. No objects! Test future methods using more than 200 records to test scalability. Use Test.startTest() and Test.stopTest() to wrap any async apex methods in unit tests.

Batch Apex

public class MyBatchClass implements Database.Batchable<sObject> {
    public (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT ... FROM ...');
    }
    public void execute(Database.BatchableContext bc, List<P> records){
        // process each batch of records
    }
    public void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }
}

Test batch apex with no more than 200 records because test can execute only one batch. Invoke a batch class:

MyBatchClass myBatchObject = new MyBatchClass();
Id batchId = Database.executeBatch(myBatchObject);

Queueable Apex

public class SomeClass implements Queueable {
    public void execute(QueueableContext context) {
        // awesome code here
    }
}

Common scenario for the queueable apex: take a set of sObject records, make a callout to an external REST endpoint or perform some calculations and then update them in the database asynchronously. Execute queueable apex:

UpdateParentAccount updateJob = new UpdateParentAccount(accounts, parentId);
// enqueue the job for processing
ID jobID = System.enqueueJob(updateJob);

Chaining jobs:

public class FirstJob implements Queueable {
    public void execute(QueueableContext context) {
        // Awesome processing logic here
        // Chain this job to next job by submitting the next job
        System.enqueueJob(new SecondJob());
    }
}

A queueable job can have only one child. But there is no limit to the depth of chaining but for Developer orgs and Trials it's 5 (1 parent job + 4 chain jobs).

You can't chain jobs in unit tests.

Schedule Apex Jobs

public class SomeClass implements Schedulable {
    public void execute(SchedulableContext ctx) {
        // awesome code here
    }
}

SchedulableContext returns a CronTrigger. How to start? String id = System.schedule('any name of the job', CRON_EXP, new SomeClass());

Note: scheduled apex job runs at SYNCHRONOUS governor limits!

Limits

50 jobs to the queue with System.enqueueJob in a single transaction. 100 scheduled Apex jobs at one time. 100 batch jobs in an apex flex queue.

Monitor async Apex

Setup -> Jobs -> Apex Jobs Setup -> Jobs -> Apex Flex Queue (Future apex is not part of flex queue)

SOQL Query on AsyncApexJob:

AsyncApexJob jobInfo = [SELECT Status, NumberOfErrors FROM AsyncApexJob WHERE Id = :jobID];

SOQL Query for scheduled job runs on CronTrigger object:


CronTrigger ct = [SELECT TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :jobID];
⚠️ **GitHub.com Fallback** ⚠️