function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel KDaniel K 

First Error: Apex CPU Limit Exceeded

Hi All,
           Today I got "First Error: Apex CPU Limit Exceeded" error while executing batch apex.

            Not even a single batch got completed, below is the status of Apex Job.
            As it has failed in the first step itself, should we consider the failure is at START method ?       
Job Type	  Status	        Status Detail	                   Total Batches	Batches Processed	Failures	
 ----------   -------   -----------------------------------------  --------------   -----------------   --------
 Batch Apex	  Failed	First error: Apex CPU time limit exceeded       0	              1	                1

Thanks
 
Daniel KDaniel K
User-added image
Daniel KDaniel K
global class BatchApex1 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) {
        //code
    }
     
    global void finish(Database.BatchableContext BC) {
        //code

    }  
}
 
<Saket><Saket>
Hi Daniel

I think u should share the code so that we can help more better

Thanks 
Saket Sharma
Aditya MohanAditya Mohan
Hi Daniel,

You are getting the CPU time error mostly because you are lot of inner queries in your class.

You can try decreasing the batch size to say 50 and try running the batch again.

Here is a documentation provided by Salesforce to avoid the error  : 

https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1