Last Updated on January 21, 2022

Why Future?

If you want to run processes in a separate thread, at a later time and want to execute the code asynchronously, we need to use future methods.

When it is used?

These are used for long-running operations such as callouts to external Web services or any operation you would like to run in its own thread

Why Queueable Apex?

Like the future is also used to execute the asynchronous code, and it enables you to add jobs in a queue and enhance the way of running asynchronous code in comparison to the future method.

When it is used?

For Apex processes that run for a long time, such as extensive database operations or external Web service callouts, you can run them asynchronously by implementing the Queueable interface

Also Read: How to Implement Salesforce SSO Using OAuth?

Differences between Future and Queueable Apex:

Future Method Queueable Job
1. Future will never use to work on SObjects or object types.

2. When using the future method we cannot monitor the jobs which are in process.

3. The future method cannot be called inside the future or batch class.

4. The future method will never be queued.

1. Queueable Jobs can contain the member variable as SObjects or custom Apex Types.

2. When using queueable jobs it will make the AsyncApexJob which we can monitor like Scheduled jobs.

3. Queueable Apex can be called from the future and batch class.

4. Using Queueable Apex will chain up to queueable jobs and in Developer Edition it is only 5 Jobs.

Queueable Apex

The Queueable class is a class that is created by implementing the pre-defined Interface “Queueable”. This interface enables the way of running your asynchronous jobs or code of job in comparison to the future method. It will enable you to monitor the jobs which are in the queue (this queue is created by Queueable interface so that the jobs can run asynchronously).

Syntax:

public class <Name of Queueable Class> implements Queueable {
//This is the only method which is contained by the Queueable Interface and required to inherit this method in Queueable class.
public void execute (QueueableContext context) {
//Code which needs to be executed
}
}

Any Apex process which takes a long time in execution, such as extensive database operations or external Web service callouts, you can run them asynchronously by implementing the Queueable interface and adding a job to the Apex job queue.

Your asynchronous apex job runs in the background in its own thread and doesn’t delay the execution of your main Apex logic. Each queued job runs when system resources become available. A benefit of using the interface methods is that some governor limits are higher than for synchronous Apex, such as heap size limits.

Future Apex

Future apex is used to run processes in a separate thread, later when system resources become available. For using future apex we do not need to create any class we just need to use “@future” annotation to identify methods that run asynchronously and to denote that the method described below this annotation is a future method.

When using synchronous processing, all method calls are made from the same thread that is executing the Apex code, and no additional processing can occur until the process is complete. You can use future methods for any operation you’d like to run asynchronously in its own thread. This provides the benefits of not blocking the user from performing other operations and providing higher governor and execution limits for the process.
Syntax:

global class <ClassName> {
//Below is future annotation which defines the given method is future method.
@future
public static void <FutureMethodName>(List<Id> recordIds) {
List<Contact> accounts = [Select Id, FirstName, LastName from Contact Where Id IN :recordIds];
// process contacts records to do awesome stuff
}
}

Also Read: How To Connect Pipedrive and Salesforce Integration?

Future methods are typically used for

Callouts to external Web services. If you are making callouts from a trigger or after performing a DML operation, you must use a future or queueable method. A callout in a trigger would hold the database connection open for the lifetime of the callout.

Operations you want to run in their own thread when time permits such as some sort of resource-intensive calculation or processing of records

Note: Reason of Objects cannot be passed as arguments to future method because the object can change between the time you call the method and the time that it actually executes. Future methods run when system resources become available. When a future method executed it may have old object value, which can cause all sorts of bad things to happen.

Things to Remember

  • The future method must be static
  • The return type must be void
  • The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types
  • You can call future methods the same way you call any other method. However, a future method cannot call another future method
  • No more than 50 method calls per Apex invocation
  • Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs
hire salesforce developers

Example for Future Method and Queueable Class

Here I am getting the value of 4 currencies in the Canadian dollar. For GBP, USD, AUD, CAD. And saving the value to custom metadata currency. For HTTPREQUEST here I have used the Future method. And because it is updating multiple records at the same time while running the batch for that I have used Queueable Apex which is updating multiple records at a time.Future Method and Queueable Class

Custom Updating of Custom Metadata

Custom Updating of Custom Metadata

Queueable Class

Queueable Class

Calling Future method and sending the list to update in Queueable class

Calling Future method

Emizentech is an authentic and experienced salesforce consulting company and our adept salesforce developers can provide you professional assistance for your next salesforce project.

Also Read: How to Create a Salesforce Lightning Map Component?

Avatar photo
Author

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.

whatsapp