Batch Apex – First error: Apex CPU time limit exceeded

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags with 5 Comments on Batch Apex – First error: Apex CPU time limit exceeded

Lessons learned while fixing error in Batch Apex , First error: Apex CPU time limit exceeded

In this post, I would share my recent experience in fixing “CPU time limit” error in Batch apex and reason. When I encountered this error, initially thought that it would be easy to fix by following some of basic rules like :

  1. Remove unnecessary code and loops
  2. Efficient use of collections (Set, Hashmap or List)
  3. Avoid using loop within loop
  4. SOQL query should be indexable
  5. Avoid unnecessary re-initialization of variables
  6. Use Aggregated SOQL (as Database operations not counted in this limit, avoid arithmetic operations in Apex)
  7. Check how much time Workflow rules and process builders are taking
  8. Is there any manage package performing heavy operations in transaction

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 tried to get a log, every attempt failed. I was thinking that problem could be in execute method however Batch Apex was failing with no debug logs. I tried all my tricks to get debug log with no success.  Batch Apex was using Query locator and it could fetch up to 50 millions of record and therefore overlooked start method.

Below pseudo code will give you some idea about Batch Apex structure and data volume.

global class SampleBatchApex_CPUError implements Database.Batchable<sObject> {

	String query = ' SELECT Field1, Field2, (SELECT Name, Identifier__c FROM childObject__r Where type=\'some value\') FROM Contact'; 
	
	global Database.QueryLocator start(Database.BatchableContext BC) { 
		return Database.getQueryLocator(query);
	}

	global void execute(Database.BatchableContext BC, List<Contact> contacts) { 
		//Some code about 100 lines
	}
	
	global void finish(Database.BatchableContext BC) {
		//Perform cleaning and start another Batch job
	}	
}

Data volume in full copy sandbox were like

  • Contact – 270k+
  • ChildObject__c – 270k+

Reason for CPU time out (in my case) – SubQuery

I tried SOQL Query Plan and run same 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 start method and queried again in execute method. And voilla…. 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.

Lessons learned

  1. 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 my colleagues of previous projects to check code for subquery, where they are getting CPU error.
  2. If batch apex error says “First error – some error” , that means error is in start method and don’t even try to hunt for execute method debug logs.

Related posts

    5 thoughts on “Batch Apex – First error: Apex CPU time limit exceeded”

    1. Hi Jitendra,

      Would you be able to provide insight on 7 and 8?:
      Check how much time Workflow rules and process builders are taking
      Is there any manage package performing heavy operations in transaction

      How are you able to determine time consumed by Workflow Rules and Process Builders? How do you determine which managed packages are performing heavy transactional operations?

      Thank You,
      Danny

    2. hello Jithendra ,

      can you tell me how did you fix this part in start method and made it working . such that we can also know how to fix this issues .

      String query = ‘ SELECT Field1, Field2, (SELECT Name, Identifier__c FROM childObject__r Where type=\’some value\’) FROM Contact’;

    3. I read somewhere that database operations do not count against CPU time and here we are talking about sub queries causing it. Could you pl elaborate on this?

      Are you talking about sub query results getting processed in some loop which is firing main query?

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.