This document discusses the evolution of asynchronous Apex processing in Salesforce, including @future, Queueable, and Batch Apex. It provides code examples of implementing these different asynchronous patterns. It highlights how features like the Flex Queue and enhancements to @future and Batch Apex have expanded asynchronous processing capabilities over time. It also notes some considerations like job ordering and management that developers should keep in mind when leveraging asynchronous Apex at scale.
4. About
GREAT ALONE. BETTER TOGETHER.
• Native to Salesforce1™ Platform
since 2009
• Investors include Salesforce Ventures
• 650+ employees, San Francisco based
4
5. Heavy lifting
• Normal Execution limited by Apex
Governors
• Number of records to process
• CPU Time
• Heap Size
Working Synchronously
8. Execute when there are
available resources
@future
Process a higher
number of records
Increased Governor
Limits in Async
We don’t have job id for @future jobs
as @future does not return anything
The method does not necessarily execute in
the same order is called
We can’t monitor @future jobs
Parameters must be primitive data types
9. @future - show me the code!
public with sharing class FutureClass {
@future
static void myMethod(String a, Integer i) {
System.debug(’Primitive variable' + a + ' and ' + i+’But I run ASYNCHROUNOUSLY');
// ALL THE LOGIC HERE
}
}
10. We can track/monitor the batch jobs:
DataBase.executeBatch returns Id
We can only run 5 concurrent jobs
Batch Apex
We can chain batch jobs
Possibility to use iterator and process
different objects (or none)
We cannot reorder or set priorities
Process up to 50M records
11. Batch Apex - show me the code!
public class UpdateAccountFields implements Database.Batchable<sObject>{
public final String Query; public final String Entity;
public final String Field; public final String Value;
public UpdateAccountFields(String q, String e, String f, String v){
Query=q; Entity=e; Field=f;Value=v;
}
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC,
List<sObject> scope){
for(Sobject s : scope){s.put(Field,Value);
} update scope;
}
public void finish(Database.BatchableContext BC){
}
}
Simple Batch Apex Examples
14. The evolution of @future - Queueable
public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
Account a = new Account(Name='Acme',Phone='(415) 555-1212');
insert a;
}
}
ID jobID = System.enqueueJob(new AsyncExecutionExample());
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE
Id=:jobID];
15. The evolution of @future - Queueable
public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
Account a = new Account(Name='Acme',Phone='(415) 555-1212');
insert a;
}
}
ID jobID = System.enqueueJob(new AsyncExecutionExample());
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE
Id=:jobID];
Supported but
undocumented
16. The evolution of @future – Enhanced Futures (pilot)
@future (limits=2xHEAP)
public static void myMemoryHog() { }
@future (limits=3xCPU)
public static void myIntenseLogicalProcessing() { }
Blogged June 2014: Bigger Apex Limits with Enhanced Futures
23. The further evolution of Batch Apex
Spring 15
Flex Queue
introduced
95 Batch Apex jobs
in the Flex Queue
waiting to be
processed + 5
concurrent Batch
Apex processes
Reorder Flex Queue
items via a Flex
Queue UI
Summer 15
Reorder Flex Queue
items
programmatically
(pilot)
Winter 16
Reorder Flex Queue
items
programmatically
GA
???
Programmatically
determine current
Flex Queue order
Queueable jobs in
Flex Queue
24. What can Flex Queue
Do for us?
1. Queue up to 100 jobs rather than killing any more than 5
2. Batch Apex no longer limited to admins – if you build an App for your users
3. We can implement our own prioritization mechanism
26. …. Remember, with great power, comes great responsibility
1. Consider the effect of new job status value on existing Batch management
code
2. Consider the possibility of jobs never being processed
3. Processes may be dependent on one another - strategy for chaining jobs in
the Flex Queue
4. Multiple processes managing the Flex Queue (currently no ability to read
the current order)
27. Recap
1. Having Several Async Processes
1. @future
2. Queueable
3. Batch Jobs
2. What to do now? Which one to use? Always batch?
1. You should use the one that better fits to your necessity …. The power is now
in your hands!!