{"id":5515,"date":"2016-06-01T18:03:39","date_gmt":"2016-06-01T18:03:39","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=5515"},"modified":"2016-06-01T18:03:39","modified_gmt":"2016-06-01T18:03:39","slug":"batch-apex-first-error-apex-cpu-time-limit-exceeded","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/batch-apex-first-error-apex-cpu-time-limit-exceeded\/","title":{"rendered":"Batch Apex &#8211; First error: Apex CPU time limit exceeded"},"content":{"rendered":"<p style=\"text-align: justify;\">In this post, I would share my recent experience in fixing &#8220;CPU time limit&#8221; error in Batch apex and reason. When I encountered this error, initially\u00a0thought that it would be easy to fix by following some of basic rules like :<\/p>\n<ol>\n<li>Remove unnecessary code and loops<\/li>\n<li>Efficient use of collections (Set, Hashmap or List)<\/li>\n<li>Avoid using loop within loop<\/li>\n<li>SOQL query should be indexable<\/li>\n<li>Avoid unnecessary re-initialization of variables<\/li>\n<li><a href=\"https:\/\/help.salesforce.com\/apex\/HTViewSolution?id=000232681&amp;language=en_US\">Use Aggregated SOQL<\/a> (as Database operations not counted in this limit, avoid arithmetic operations in Apex)<\/li>\n<li>Check how much time Workflow rules and process builders are taking<\/li>\n<li>Is there any manage package performing heavy operations in transaction<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">This piece of code was handed over to me from previous team, so I was not fully aware about full functionality and thought to check debug logs. To my surprise, how many times I\u00a0tried to get a log, every attempt failed.\u00a0I was thinking that problem could be in <em>execute<\/em> method however\u00a0Batch Apex was failing with no debug logs. I tried all my tricks to get <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/advance-apex-debugging-in-salesforce-and-best-practices-videos\/\">debug log<\/a> with no success. \u00a0Batch Apex was using Query locator and it could fetch up to <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_batch_interface.htm\">50 millions of record<\/a> and therefore overlooked <em>start<\/em> method.<!--more--><\/p>\n<p style=\"text-align: justify;\">Below pseudo code will give you some idea about Batch Apex structure and data volume.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nglobal class SampleBatchApex_CPUError implements Database.Batchable&lt;sObject&gt; {\r\n\r\n\tString query = ' SELECT Field1, Field2, (SELECT Name, Identifier__c FROM childObject__r Where type=\\'some value\\') FROM Contact'; \r\n\t\r\n\tglobal Database.QueryLocator start(Database.BatchableContext BC) { \r\n\t\treturn Database.getQueryLocator(query);\r\n\t}\r\n\r\n\tglobal void execute(Database.BatchableContext BC, List&lt;Contact&gt; contacts) { \r\n\t\t\/\/Some code about 100 lines\r\n\t}\r\n\t\r\n\tglobal void finish(Database.BatchableContext BC) {\r\n\t\t\/\/Perform cleaning and start another Batch job\r\n\t}\t\r\n}\r\n<\/pre>\n<p>Data volume in full copy sandbox were like<\/p>\n<ul>\n<li>Contact &#8211; 270k+<\/li>\n<li>ChildObject__c &#8211; 270k+<\/li>\n<\/ul>\n<p><strong>Reason for CPU time out (in my case) &#8211; SubQuery<\/strong><\/p>\n<p style=\"text-align: justify;\">I tried SOQL Query Plan and run\u00a0same query in Developer console and workbench, but it was not timing out. Still, gave one shot to idea that I had and tried to remove subquery from\u00a0<em>start<\/em> method and queried again in <em>execute<\/em> method. And <em>voilla<\/em>&#8230;. it worked. So, subquery in SOQL was causing CPU time limit error and it was not even able to enter in execute method. I got lost because there were no debug logs. Instead of spending time on actual issue, I spent lot of time to figure out why debug log is not showing up.<\/p>\n<p><strong>Lessons learned <\/strong><\/p>\n<ol>\n<li style=\"text-align: justify;\">If we are getting CPU time limit error (in my experience I have seen many), then avoid using sub-query, its costly and time consuming. Damn !!! I need to tell\u00a0my colleagues of previous projects to check code for subquery, where they are getting CPU\u00a0error.<\/li>\n<li style=\"text-align: justify;\">If batch apex error says &#8220;First error &#8211; some error&#8221; , that means error is in <em>start<\/em> method and don&#8217;t even try to hunt for <em>execute<\/em> method debug logs.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Lessons learned while fixing error in Batch Apex , First error: Apex CPU time limit exceeded<\/p>\n","protected":false},"author":1,"featured_media":0,"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],"class_list":["post-5515","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-batch-apex"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6244,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/a-tale-of-governor-limits-and-solutions-in-salesforce\/","url_meta":{"origin":5515,"position":0},"title":"A Tale of Governor Limits and Solutions in Salesforce","author":"Jitendra","date":"November 21, 2017","format":false,"excerpt":"Thrilling story of Salesforce Technical Architect on a quest to solve application problems and avoid governor limit errors","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"A Tale of Governor Limits and Solutions in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":31927,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-scale-center-complete-guide-performance\/","url_meta":{"origin":5515,"position":1},"title":"Salesforce Scale Center Complete Guide: Performance Monitoring &#038; Optimization | March 2026","author":"Jitendra","date":"March 12, 2026","format":false,"excerpt":"Master Salesforce Scale Center for performance monitoring and optimization. Learn setup, ApexGuru integration, concurrent Apex analysis, and best practices for enterprise scalability with 9 analysis report types.","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce Scale Center Infographic - Free self-serve performance monitoring platform with 9 report types, 30-day data retention, and 10-12 minute latency for Enterprise, Unlimited, and Professional Edition orgs","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/03\/Salesforce-Scale-Center-Complete-Guide-infographic.png?fit=1200%2C1148&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/03\/Salesforce-Scale-Center-Complete-Guide-infographic.png?fit=1200%2C1148&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/03\/Salesforce-Scale-Center-Complete-Guide-infographic.png?fit=1200%2C1148&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/03\/Salesforce-Scale-Center-Complete-Guide-infographic.png?fit=1200%2C1148&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/03\/Salesforce-Scale-Center-Complete-Guide-infographic.png?fit=1200%2C1148&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":6836,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/framework-to-fix-governor-limit-of-100-jobs-in-flex-queue\/","url_meta":{"origin":5515,"position":2},"title":"Framework to fix &#8211; Governor Limit of 100 jobs in Flex Queue","author":"Jitendra","date":"April 25, 2019","format":false,"excerpt":"How to design an application in Salesforce to handle governor limit error of maximum 100 jobs in Flex Queue","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Apex Flex Queue Governor Limit","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Salesforce-Apex-Flex-Queue-Governor-Limit.png?fit=1200%2C539&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Salesforce-Apex-Flex-Queue-Governor-Limit.png?fit=1200%2C539&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Salesforce-Apex-Flex-Queue-Governor-Limit.png?fit=1200%2C539&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Salesforce-Apex-Flex-Queue-Governor-Limit.png?fit=1200%2C539&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/04\/Salesforce-Apex-Flex-Queue-Governor-Limit.png?fit=1200%2C539&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3025,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-11\/","url_meta":{"origin":5515,"position":3},"title":"Salesforce Interview Questions \u2013 Part 11","author":"Jitendra","date":"August 19, 2012","format":false,"excerpt":"101. How to force lead assignment rule via Apex while updating or adding the Lead? Ans : To enforce Assignment Rules in Apex you will need to perform following steps: Instantiate the \"Database.DMLOptions\"\u009d class. Set the \"useDefaultRule\"\u009d property of \"assignmentRuleHeader\"\u009d to True. Finally call a native method on your Lead\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Pagination in SOQL using keyword Offset","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":5515,"position":4},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. [java] Date dt = Date.today().addDays(7); Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting',\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3278,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/visualforce\/using-fieldset-with-visualforce-and-apex\/","url_meta":{"origin":5515,"position":5},"title":"Using FieldSet with Visualforce and Apex","author":"Jitendra","date":"May 3, 2013","format":false,"excerpt":"One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its \"Maintenance\"\u009d, if New Filed is Added or needed to remove field we have to modify our code every time. However, Thanks to Salesforce that we have \"Field Set\"\u009d. With the\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Create Field Set in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5515","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=5515"}],"version-history":[{"count":4,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5515\/revisions"}],"predecessor-version":[{"id":5519,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5515\/revisions\/5519"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=5515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=5515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=5515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}