How to resolve ‘Apex CPU time limit exceeded’ error in Salesforce?
1. About the ‘Apex CPU time limit exceeded’ error
Salesforce has a timeout limit for transactions based on CPU usage. If transactions consume too much CPU time, they will be shut down as a long-running transaction.
CPU Timeout Governor Limit:
- Synchronous Process: 10 seconds
- Asynchronous process: 60 seconds
Note: If you started getting ‘Apex CPU time limit exceeded’ error in production and if any of your key features are impacted, you can request Salesforce to increase the CPU time limit for few days/weeks which will give you required time to deploy a fix.
1.1. What is counted
- All Apex code
- Library functions exposed in Apex
- Workflow execution
1.2. What is not counted
- Database operations, e.g. the portion of execution time spent in the database for DML, SOQL, and SOSL
- Waiting time for Apex callouts
- SOQL
2. How to reduce CPU timeout?
2.1. Using Map based query
List<Lead> lstacc=[Select Id from Lead limit 2000];
Set<Id> setIds=new Set<Id>();
for(Lead tmpLead:leadList){ //More CPU time for sure due to looping
setIds.add(tmpLead.id);
}
//Using Map query saves CPU time
//Fetching all Leads in map
Map<Id,Lead> leadMap = new Map<Id,Lead>([Select Id from Lead limit 2000]);
//Creating list of Leads
List<Lead> leadList = leadMap.values() ;
//Creating set of ids
Set<id> leadIdSet = leadMap.keySet() ;
2.2. Asynchronous Processing
CPU time out limit for asynchronous process is 60seconds(6X of synchronous process). If the results of the operation are not required in real time consider moving logic to asynchronous processing.
Asynchronous processing comes in a number of different flavors.
Type | Overview | Common Scenarios |
---|---|---|
Future Methods | Run in their own thread, and do not start until resources are available. | Web service callout. |
Batch Apex | Run large jobs that would exceed normal processing limits. | Data cleansing or archiving of records. |
Queueable Apex | Similar to future methods, but provide additional job chaining and allow more complex data types to be used. | Performing sequential processing operations with external Web services. |
Scheduled Apex | Schedule Apex to run at a specified time. | Daily or weekly tasks. |
Platform Events | Connect business processes in Salesforce and external apps through the exchange of real-time event data | Communicate with external systems. |
2.3. Aggregate SOQL
Database time is not calculated in CPU time. Using aggregate SOQL for your business use case will have reduce CPU time.
2.4. Only take necessary data and run a loop
Filter only specific data while doing a for on a list of records.
3. How to debug
You can figure out which code is consuming more time using Analysis Perspective in Developer Console Log Inspector.
You can access it via Debug -> Perspective Manager
Select Analysis and Click on Open (The debug log must be open to enable Open button)
Timeline Tab shows which CPU time consumed by Apex Code, Workflow, DB etc..
You can also view the CPU time various methods and SOQL queries.