Salesforce Future Method vs Queueable Apex

An asynchronous process is a process or function that executes a task "in the background" without the user having to wait for the task to finish.


3 min read
Salesforce Future Method vs Queueable Apex

An asynchronous process is a process or function that runs "in the background" without the user having to wait for it to finish.Salesforce provides the capability to run code asynchronously.

It is beneficial to consider running certain processes in the asynchronous context if the result of the process is not critical to the transaction at hand . Salesforce , applies a more lenient set of governor limits for asynchronous transactions

Future methods & Queueable methods are instruments within the Apex coding framework with allow asynchronous Apex.

Future Method

A future method runs in the background, asynchronously. Each future method is queued and executes independent from the original transaction as and when system resources become available.

Syntax

@future
public static void myFutureMethod()
{   
     // Perform some operations
}
  • Use @ future annotation to define a future method.
  • The future method must be defines as static and can only return a void return type. As future methods run asynchronously, the code which invokes it would continue executing and would not wait for the future method call to finish. This explains why future methods don't have a return type.
@future(callout=true)

Use (callout = true) to make a callout from the future method.

Benefits

  • They promote parallelism and are especially useful for long-running operations (Such as a callout with a significant wait time). In such scenarios , the callout would be made in the background as resources become available.
  • They can be used to prevent Mixed DML exception by isolating the DMLs on setup objects (such as user) and non setup object (such as account) in separate transactions.
  • Async Apex execute with a more relaxed governor limits. For Instance,  SOQL query limits and heap size limits and much higher for future transactions.

Limitations

  • Future methods only support primitive datatypes as arguments . A Future method cannot take sObjects or objects as arguments- This is because the sObject value might change between the time you call the method and the time it executes.
  • We cannot invoke a future method from another future or batch apex.
  • There can be only 50 future methods for single apex transaction.

Queueable Apex

Queueable Apex is a more recent and efficient way of running your Apex code asynchronously as compared to using future methods.

Syntax

public class QueueableClass implements Queueable {
    public void execute(QueueableContext context) {
        Account a = new Account(Name='ABC',Phone='8800886464');
        insert a;        
    }
}
  • Implement the Queueable interface create a Queueable apex class.
  • The interface has only 1 method called 'execute' , which needs to be implemented.

Enqueue Job

 ID jobID = System.enqueueJob(new QueueableClass())

One a queueable class is defined , the above code is used to enqueue the job in the queue and this will be processed when system resources become available.

Unlike future methods, you can potentially invoke an enqueueJob from another enqueueJob - This allows for a chaining of enqueueJobs allowing a series of asynchronous processes to run one after the other.  

Query Job

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

You can use the above query to check the status of an Asynchronous by querying it.

Benefits

  • Job ID: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the job submitted. This ID can then be used to query an monitor the status of an asynchronous job.
  • Non-primitive types: Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. This was not possible in the future method.
  • Chaining jobs: You can chain one job to another job by enqueuing a second job within the body of the first job. Chaining jobs is useful if your process depends on another process to have run first.
 public void execute(QueueableContext context) {
     
        // Chain this job to next job by submitting the next job
        System.enqueueJob(new SecondJob());
 }

Implement Database.AllowsCallouts interface to make callout from Queueable class.

Limitations

  • You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
  • While you are allowed to chain multiple enqueueJobs , you can only invoke a single enqueueJob from a specific code entity like a class or trigger. Invoking multiple child jobs from the same queueable job isn’t supported.

Related Articles

Skinny Table in Salesforce
2 min read
Salesforce - Indexes & Query optimizer
3 min read
PK chunking in Salesforce
2 min read

GO TOP