{"id":6836,"date":"2019-04-25T00:36:37","date_gmt":"2019-04-25T04:36:37","guid":{"rendered":"https:\/\/www.jitendrazaa.com\/blog\/?p=6836"},"modified":"2019-05-06T08:16:58","modified_gmt":"2019-05-06T12:16:58","slug":"framework-to-fix-governor-limit-of-100-jobs-in-flex-queue","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/framework-to-fix-governor-limit-of-100-jobs-in-flex-queue\/","title":{"rendered":"Framework to fix &#8211; Governor Limit of 100 jobs in Flex Queue"},"content":{"rendered":"\n<p class=\"justify\">Recently , I inherited an Org with huge customization , which enqueue 100+ Batch Jobs in few scenario. Now, don&#8217;t ask me why ? <\/p>\n\n\n\n<p class=\"justify\">I remember, few years back, Salesforce had limit of 5 Apex Batch that can be executed at a time, but we had expectation and demand !!! Salesforce introduced Flex queue and we can have 100 Batch apex waiting to be executed, still we are not happy. After all, human is wanting animal. <\/p>\n\n\n\n<p class=\"justify\">After 100 Batch apex, all jobs were failing with error <strong>System.AsyncException You have exceeded the limit of 100 Jobs in the flex queue. <\/strong><a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/a-tale-of-governor-limits-and-solutions-in-salesforce\/\">As I explained my life previously in this post<\/a>, I had to fix this issue as well.<\/p>\n\n\n\n<p class=\"justify\">Right way to fix it was to analyze existing code, perform code review, why do we need even customization etc.. However, time was crucial and I had to do something quickly. <\/p>\n\n\n\n<p>Below framework was used to fix the issue<\/p>\n\n\n\n<!--more-->\n\n\n\n<ol class=\"justify wp-block-list\"><li>Create a Custom Object to hold information about batch<\/li><li>Use an intermediate Apex class to queue Batch Apex and Queueable Apex. If there is no availability in Flex queue then simply dump it in custom object.<\/li><li>Use a Scheduler which will run every 10 mins, check Flex queue and submit batches from Custom object if limit available<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"649\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/AsyncQueueFramework.png?resize=1024%2C649&#038;ssl=1\" alt=\"Async Queue Framework in Salesforce to address governor limits\" class=\"wp-image-6851\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/AsyncQueueFramework.png?resize=1024%2C649&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/AsyncQueueFramework.png?resize=300%2C190&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/AsyncQueueFramework.png?resize=768%2C486&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/AsyncQueueFramework.png?w=1200&amp;ssl=1 1200w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption>Async Queue Framework in Salesforce to address governor limits<\/figcaption><\/figure>\n\n\n\n<p>As you can see in below code, syntax to submit batch Apex is almost similar. <\/p>\n\n\n\n<p>Instead of<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nID batchprocessid = Database.executeBatch(batchApexInstance,200);\n<\/pre><\/div>\n\n\n<p>we would be using<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nID batchprocessid = AsyncApexFramework.submitBatch(batchApexInstance,batchSize,Prioity,isRetry);\nAsyncApexFramework.flush(); \n<\/pre><\/div>\n\n\n<p>Where , <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>@Param 1 &#8211; Instance of Batch Apex<\/li><li>@Param 2 &#8211; Batch Size \/ Scope Size<\/li><li>@Param 3 &#8211; Priority to process if there are many job in Queue. If null then default would be 99 <\/li><li>@Param 4 &#8211; if Batch Apex has error, should it be retried again ? Use this option carefully , make sure your design does not negatively impact if same Batch runs multiple time<\/li><\/ul>\n\n\n\n<p>Same way , instead of using <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nId jobId = System.enqueueJob(queueableClassInstance)\n<\/pre><\/div>\n\n\n<p>We would be using <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nId jobId = AsyncApexFramework.submitQueueable(queueableClassInstance,priority,isRetry);\n\nAsyncApexFramework.flush();\n<\/pre><\/div>\n\n\n<p>Where, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>@Param 1 &#8211; Instance of Queueable class<\/li><li>@Param 2 &#8211; Priority to process if there are many job in Queue. If null then default would be 99 <br>@Param 3 &#8211; if Batch Apex has error, should it be retried again ? Use this option carefully , make sure your design does not negatively impact if same Batch runs multiple time<\/li><\/ul>\n\n\n\n<p>Below sample code, shows how we can add 300+ job in single transaction using this framework<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/Code snippet to submit Batch and Queuable\nfor(Integer i = 0; i&lt;300;i++){\n    BatchDemo b = new BatchDemo(&#039;#B&#039;); \n\tAsyncApexFramework.submitBatch(b,1,99,true);\n    AsyncApexFramework.submitQueueable(new QueueableDemo(),99,true);\n}\nAsyncApexFramework.flush(); \n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"715\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Async-Queue-Framework-Salesforce.png?resize=1024%2C715&#038;ssl=1\" alt=\"Custom Object AsyncQueue__c\" class=\"wp-image-6854\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Async-Queue-Framework-Salesforce.png?resize=1024%2C715&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Async-Queue-Framework-Salesforce.png?resize=300%2C209&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Async-Queue-Framework-Salesforce.png?resize=768%2C536&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Async-Queue-Framework-Salesforce.png?resize=1200%2C838&amp;ssl=1 1200w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Async-Queue-Framework-Salesforce.png?w=2000&amp;ssl=1 2000w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption>Custom Object AsyncQueue__c<\/figcaption><\/figure>\n\n\n\n<p>Feel free to add your feedback and findings on this. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Source Code of Framework : <\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/JitendraZaa\/305c5d409183dd061893deff0282982d.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>How to design an application in Salesforce to handle governor limit error of maximum 100 jobs in Flex Queue<\/p>\n","protected":false},"author":1,"featured_media":6839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[368,468,425,469],"class_list":["post-6836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","tag-batch-apex","tag-framework","tag-governor-limits","tag-org9"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Salesforce-Apex-Flex-Queue-Governor-Limit.png?fit=1593%2C716&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6939,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/podcast-how-to-achieving-hyper-threading-in-salesforce\/","url_meta":{"origin":6836,"position":0},"title":"Podcast &#8211; How to Achieving Hyper-Threading in Salesforce","author":"Jitendra","date":"January 2, 2020","format":false,"excerpt":"Total number of Asynchronous Apex which includes Batch Apex, Future Method, Queueable & Scheduled job is 250k or 200 x Total User license whichever is greater. Now lets do some fact check : Execute method of Batch Apex can only call 1 Queueable Scheduler can call 50 QueueableWe can have\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Podcast - Hyper Thread iN Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5515,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/batch-apex-first-error-apex-cpu-time-limit-exceeded\/","url_meta":{"origin":6836,"position":1},"title":"Batch Apex &#8211; First error: Apex CPU time limit exceeded","author":"Jitendra","date":"June 1, 2016","format":false,"excerpt":"Lessons learned while fixing error in Batch Apex , First error: Apex CPU time limit exceeded","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4136,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-spring-15-some-highlighted-features\/","url_meta":{"origin":6836,"position":2},"title":"Salesforce Spring 15 &#8211; some highlighted features","author":"Jitendra","date":"December 28, 2014","format":false,"excerpt":"Download PDF version of release notes from here. 1. Import Account \/ Contact from 16 different sources (previously generic csv was only option to import). 2. Salesforce Analytics Query language (SAQL) We already know about SOQL and SOSL. This is new Query language for Analytics cloud to enable ad hoc\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Case Macro - Spring 15","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":5885,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-spring-17-release-my-favorite-features\/","url_meta":{"origin":6836,"position":3},"title":"Salesforce Spring 17 release &#8211; My favorite features","author":"Jitendra","date":"January 27, 2017","format":false,"excerpt":"List of my favorite features in Salesforce Spring 17","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Apex Batch job - Spring 17","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/01\/Salesforce-Apex-Batch-job-Spring-17.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/01\/Salesforce-Apex-Batch-job-Spring-17.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/01\/Salesforce-Apex-Batch-job-Spring-17.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/01\/Salesforce-Apex-Batch-job-Spring-17.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":6216,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/performing-load-testing-in-salesforce-using-selenium-and-testng\/","url_meta":{"origin":6836,"position":4},"title":"Performing Load Testing in Salesforce using Selenium and TestNG","author":"Jitendra","date":"August 28, 2017","format":false,"excerpt":"Parallel execution of browsers in Selenium with the help of TestNG and determining maximum operating capacity of custom code in Salesforce","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Selenium Salesforce load Testing","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Selenium-Salesforce-load-Testing.jpg?fit=964%2C848&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Selenium-Salesforce-load-Testing.jpg?fit=964%2C848&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Selenium-Salesforce-load-Testing.jpg?fit=964%2C848&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Selenium-Salesforce-load-Testing.jpg?fit=964%2C848&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3112,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-13\/","url_meta":{"origin":6836,"position":5},"title":"Salesforce Interview Question \u2013 Part 13","author":"Jitendra","date":"September 25, 2012","format":false,"excerpt":"121 : Consider we have overall 90% code coverage however there is one class which have 0% code coverage. Can we still able to deploy that class on production? Ans : Yes. Minimum 1% required for every trigger and there is no such restriction for Apex class. 122 : How\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=6836"}],"version-history":[{"count":10,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions"}],"predecessor-version":[{"id":6856,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions\/6856"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/6839"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=6836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=6836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=6836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}