How to Fix ‘Apex CPU time limit exceeded’ Error in Salesforce?
Overview :
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.
Solution :
Using Map based query
List<Account> lstAcc=[Select Id from Account limit 5000]; Set<Id> setIds=new Set<Id>(); for(Account tmpAcc:lstAcc){
//More CPU time for sure due to looping setIds.add(tmpAcc.id); } //Using Map query saves CPU time //Fetching all the Accounts in map Map<Id,Account> AccountMap = new Map<Id,Account>([Select Id from Account limit 5000]); //Creating list of Accounts List<Account> lstAcc = AccountMap.values() ; //Creating set of ids Set<id> setIds = AccountMap.keySet() ;
Follow Us