Get a Quote

Hey, do you want to share your amazing ideas with us, drop your requirements, we are keen to work with you on your fantastic project. We will try to reach you shortly, SEE YOU !

Tell us about you

Tell us about your Project

Future Method Vs Queueable Apex in Salesforce

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 MethodQueueable 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 ApexAsyncJobs 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

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

author profile
About Author
Author Name:
Virendra Sharma

Certified Magento 2 Consultant

Author Bio:

Virendra Sharma is an ecommerce tech-enthusiast and certified Magento Developer. With a decade of experience in Magento ecommerce development, he has been assisting ecommerce businesses across the globe to harness Magento capabilities by developing, maintaining, and improving clients’ eCommerce websites. A leader in his own rights his teammates see him as an avid researcher and a Magento evangelist.

About Emizentech:

An acclaimed IT solution provider and a leading Magento 2 development company with its expertise lies in delivering top of the line ecommerce solutions, Salesforce commerce cloud services, mobile app development and many more verticals.

Relate Contact to Multiple Contacts
27 May, 2020

How To Relate a Contact To Multiple Accounts In Salesforce

With the Salesforce Summer ’16 release, you can relate a single contact to multiple accounts, without duplication! This is a […]
Read more
Salesforce Development
Chatbots in eCommerce Industry
27 May, 2020

How AI Chatbots Are Benefiting The eCommerce Industry?

Whether you call it a revolution or an intrusion, chatbots have gained a significant place in the ecommerce practices. From […]
Read more
Ecommerce Development
Odoo ERP Integration In eCommerce Platform
26 May, 2020

Odoo ERP Integration In eCommerce Platform: Complete Guide

Who knew that shopping online within just a few clicks would be easier than preparing a cup of tea. Ecommerce […]
Read more
Ecommerce Development

Let’s Talk Business

Reach out to us by following below given details, or simply drop us a text to start a conversation, we are here to provide best in class business solutions.

SAY SOMETHING