Future Methods in Salesforce

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 / WhatCall @future methodSystem.enqueueJob()Database.executeBatch()System.schedule()
Anonymous Apex5050100100
@AuraEnabled method5050100100
@future method010100
Queueable execute()501100100
Schedulable execute()5050100100
Batch start()010100
Batch execute()010100
Batch finish()010100
Platform Event trigger5050100100
Spread the love

5 Comments

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *