{"id":2531,"date":"2023-11-20T13:15:40","date_gmt":"2023-11-20T13:15:40","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=2531"},"modified":"2023-11-20T13:15:44","modified_gmt":"2023-11-20T13:15:44","slug":"future-vs-queueable-apex","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/future-vs-queueable-apex.html","title":{"rendered":"Future Method Vs. Queueable Apex in Salesforce"},"content":{"rendered":"\n<p>In the fast-paced world of Salesforce development, understanding the best tools and methods for efficient and effective coding is crucial. This article dives into two key concepts: Future Method and Queueable Apex. Both are powerful techniques used in Salesforce to handle asynchronous operations, but they have distinct features and use cases. <\/p>\n\n\n\n<p>We&#8217;ll explore the nuances of Future Method and Queueable Apex, comparing their functionalities, advantages, and when to use each in your Salesforce projects. Whether you&#8217;re a seasoned developer or new to Salesforce, this comparison will provide valuable insights, helping you make informed decisions for optimizing your code. <\/p>\n\n\n\n<p>Stay tuned as we unravel the complexities and reveal the potential of Future Method and Queueable Apex in Salesforce!<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #007db2;color:#007db2\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #007db2;color:#007db2\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/future-vs-queueable-apex.html#Why_Future_Method\" >Why Future Method?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/future-vs-queueable-apex.html#Why_Queueable_Apex\" >Why Queueable Apex?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/future-vs-queueable-apex.html#Differences_Between_Future_and_Queueable_Apex\" >Differences Between Future and Queueable Apex:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/future-vs-queueable-apex.html#Queueable_Apex\" >Queueable Apex<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/future-vs-queueable-apex.html#Future_Apex_Method\" >Future Apex Method<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Future_Method\"><\/span>Why Future Method?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When it is used?<\/h3>\n\n\n\n<p>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<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Queueable_Apex\"><\/span>Why Queueable Apex?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When it is used?<\/h3>\n\n\n\n<p>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<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/implement-sso-using-oauth-for-salesforce.html\" target=\"_blank\" rel=\"noopener\">How to Implement Salesforce SSO Using OAuth?<\/a><\/h4>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Differences_Between_Future_and_Queueable_Apex\"><\/span>Differences Between Future and Queueable Apex:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-black-color has-pale-cyan-blue-background-color has-text-color has-background\"><thead><tr><th>Future Method<\/th><th>Queueable Apex<\/th><\/tr><\/thead><tbody><tr><td><strong>1.<\/strong> Future will never use to work on SObjects or object types. <p><strong>2.<\/strong> When using the future method we cannot monitor the jobs which are in process.<\/p> <p><strong>3.<\/strong> The future method cannot be called inside the future or batch class.<\/p> <p><strong>4. <\/strong>The future method will never be queued.<\/p><\/td><td><strong>1. <\/strong>Queueable Jobs can contain the member variable as SObjects or custom Apex Types. <p><strong>2. <\/strong>When using queueable jobs it will make the AsyncApexJob which we can monitor like Scheduled jobs.<\/p> <p><strong>3. <\/strong>Queueable Apex can be called from the future and batch class.<\/p> <p><strong>4. <\/strong>Using Queueable Apex will chain up to queueable jobs and in Developer Edition it is only 5 Jobs.<\/p><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Queueable_Apex\"><\/span>Queueable Apex<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Queueable class is a class that is created by implementing the pre-defined Interface \u201cQueueable\u201d. 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).<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class &lt;Name of Queueable Class&gt; implements Queueable {\n\/\/This is the only method which is contained by the Queueable Interface and required to inherit this method in Queueable class.\npublic void execute (QueueableContext context) {\n\/\/Code which needs to be executed\n}\n}\n<\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Your asynchronous apex job runs in the background in its own thread and doesn\u2019t 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Future_Apex_Method\"><\/span>Future Apex Method<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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 <em><strong>\u201c@future\u201d<\/strong><\/em> annotation to identify methods that run asynchronously and to denote that the method described below this annotation is a future method.<\/p>\n\n\n\n<p>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\u2019d 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.<br>\n<strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">global class &lt;ClassName&gt; {\n\/\/Below is future annotation which defines the given method is future method.\n@future\npublic static void &lt;FutureMethodName&gt;(List&lt;Id&gt; recordIds) {\nList&lt;Contact&gt; accounts = [Select Id, FirstName, LastName from Contact Where Id IN :recordIds];\n\/\/ process contacts records to do awesome stuff\n}\n}\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/pipedrive-salesforce-integration.html\" target=\"_blank\" rel=\"noopener\">How To Connect Pipedrive and Salesforce Integration?<\/a><\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">Future methods are typically used for<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Operations you want to run in their own thread when time permits such as some sort of resource-intensive calculation or processing of records<\/p>\n\n\n\n<p><strong>Note:<\/strong> 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Things to Remember<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The future method must be static<\/li>\n\n\n\n<li>The return type must be void<\/li>\n\n\n\n<li>The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types<\/li>\n\n\n\n<li>You can call future methods the same way you call any other method. However, a future method cannot call another future method<\/li>\n\n\n\n<li>No more than 50 method calls per Apex invocation<\/li>\n\n\n\n<li>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<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/multisitelocal.ezxdemo.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" class=\"wp-image-2389\"\/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example for Future Method and Queueable Class<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/1-Future-Method-and-Queueable-Class.png\" alt=\"Future Method and Queueable Class\" class=\"wp-image-2555\"\/><\/figure><\/div>\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Updating of Custom Metadata<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/2-Custom-Updating-of-Custom-Metadata.png\" alt=\"Custom Updating of Custom Metadata\" class=\"wp-image-2556\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Queueable Class<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/3-Queueable-Class.png\" alt=\"Queueable Class\" class=\"wp-image-2557\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Calling Future method and sending the list to update in Queueable class<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/4-Calling-Future-method.png\" alt=\"Calling Future method\" class=\"wp-image-2558\"\/><\/figure><\/div>\n\n\n<p>Emizentech is an authentic and experienced <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\">salesforce consulting company<\/a> and our adept <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">salesforce developers<\/a> can provide you professional assistance for your next salesforce project.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-salesforce-lightning-map-component.html\" target=\"_blank\" rel=\"noopener\">How to Create a Salesforce Lightning Map Component?<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>In the fast-paced world of Salesforce development, understanding the best tools and methods for efficient and effective coding is crucial. This article dives into two key concepts: Future Method and Queueable Apex. Both are powerful techniques used in Salesforce to handle asynchronous operations, but they have distinct features and use cases. We&#8217;ll explore the nuances<\/p>\n","protected":false},"author":39,"featured_media":40443,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-2531","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Future-Method-Vs.-Queueable-Apex-in-Salesforce-600x400.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Future-Method-Vs.-Queueable-Apex-in-Salesforce-600x408.jpg","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=2531"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2531\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/40443"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=2531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=2531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=2531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}