Future Methods in Salesforce

future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time. You can also use future methods to isolate DML operations on different sObject types to prevent the mixed DML error. Each future method is queued and executes when system resources become available. That way, the execution of your code doesn’t have to wait for the completion of a long-running operation. A benefit of using future methods is that some governor limits are higher, such as SOQL query limits and heap size limits.

Future method syntax:
global class FutureClass {
    @future
    public static void myFutureMethod() {   
         // Perform some operations
    }
}
  • Use @future annotation before the method declaration.
  • Future methods must be static methods, and can only return a void type.
  • The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
  • Future methods can’t take standard or custom objects as arguments.
  • A common pattern is to pass the method a list of record IDs that you want to process asynchronously
global class FutureMethodRecordProcessing {
    @future
    public static void processRecords(List<ID> recordIds){   
    // Get those records based on the IDs
    List<Account> accts = [SELECT Name FROM Account WHERE Id IN :recordIds];
    // Process records
    }
}

Future method callouts using @future(callout=true):

To allow callout from the future method annotation needs an extra parameter (callout=true) to indicate that callouts are allowed. Here is example

global class FutureMethodExample {
	@future(callout=true)
	public static void getStockQuotes(String acctName){
	// Perform a callout to an external service
	}
}