Introduction
Future methods are another way to execute code asynchronously in Salesforce. They are defined using the @future
annotation.
Key Features
- Allows asynchronous execution.
- Supports operations that donβt need to return a result.
- Useful for callouts and handling bulk DML operations.
Syntax
global class FutureExample {
@future
public static void updateAccounts(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
for (Account acc : accounts) {
acc.Name = acc.Name + ' - Future Updated';
}
update accounts;
}
}
Calling a Future Method
Set<Id> accountIds = new Set<Id>{'001xx000003DG0oAAO', '001xx000003DG0pAAO'};//sample ids
FutureExample.updateAccounts(accountIds);
Limitations
- Limited to 50 calls per transaction.
- Cannot return values.
- Cannot use
@future
methods inside other@future
, batch.
Limitation for Asynchronous
From / What | Call @future method | System.enqueueJob() | Database.executeBatch() | System.schedule() |
---|---|---|---|---|
Anonymous Apex | 50 | 50 | 100 | 100 |
@AuraEnabled method | 50 | 50 | 100 | 100 |
@future method | 0 | 1 | 0 | 100 |
Queueable execute() | 50 | 1 | 100 | 100 |
Schedulable execute() | 50 | 50 | 100 | 100 |
Batch start() | 0 | 1 | 0 | 100 |
Batch execute() | 0 | 1 | 0 | 100 |
Batch finish() | 0 | 1 | 0 | 100 |
Platform Event trigger | 50 | 50 | 100 | 100 |
Clearly explained the content well.
Thanks
Keep Posting more useful concepts like this …
πππ
This article is very useful for understanding the concept and i like the Limitation for Asynchronous table it is very easy to learn, I was searching for many resources and i couldn’t find any, i hope you post many concepts like this.