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
SN SFSN SF 

Apex CPU time limit exceeded - VF Page Error

Hello ,
I have a VF page page that works fine when I try to update 10-15 records at a time . But I get Exception message when I try to update 20+ records . When I checked the Debug logs , I could see below Error message ' Apex CPU time Limit execced . 
 APEX CPU TIME LIMIT EXCEEDED

Although to overcome this I have implemented Pagination , so that user can adjust the page size and update lesser records at a time.
But I just want to know  -
1. The reason behind hitting this CPU time Limit Error ?
2. Other efficient ways to resolve this issue ?
3. What is best approach OR procedure to be following to display and process BULK records through VF page 

Can someone please guide me on this?

Thanks,
SN
Best Answer chosen by SN SF
SwethaSwetha (Salesforce Developers) 
HI SN,

1. This error generally occurs if transactions consume too much CPU time. Salesforce has a timeout limit for transactions based on CPU usage. If transactions consume too much CPU time, Salesforce shut them down as a long-running transaction.

2. To fix the issue, you need to optimize the code involved following the best practices described in the below articles.

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

3. Please use the following limit methods in your code to debug the amount of CPU time currently used in the transaction:

getCpuTime()
Returns the CPU time (in milliseconds) accumulated on the Salesforce servers in the current transaction.

getLimitCpuTime()
Returns the time limit (in milliseconds) of CPU usage in the current transaction.

Related:https://salesforce.stackexchange.com/questions/51475/how-do-i-create-update-bulk-records-in-one-execution-synchronously

which suggests 50k records is an API limit and problem with using pagination is OFFSET maximum value is 2000 so the greatest number of records you could query in this way would be 52,000

https://salesforce.stackexchange.com/questions/319277/how-to-debug-which-process-is-slowing-my-updates explains how to debug this issue using developer console


Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI SN,

1. This error generally occurs if transactions consume too much CPU time. Salesforce has a timeout limit for transactions based on CPU usage. If transactions consume too much CPU time, Salesforce shut them down as a long-running transaction.

2. To fix the issue, you need to optimize the code involved following the best practices described in the below articles.

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

3. Please use the following limit methods in your code to debug the amount of CPU time currently used in the transaction:

getCpuTime()
Returns the CPU time (in milliseconds) accumulated on the Salesforce servers in the current transaction.

getLimitCpuTime()
Returns the time limit (in milliseconds) of CPU usage in the current transaction.

Related:https://salesforce.stackexchange.com/questions/51475/how-do-i-create-update-bulk-records-in-one-execution-synchronously

which suggests 50k records is an API limit and problem with using pagination is OFFSET maximum value is 2000 so the greatest number of records you could query in this way would be 52,000

https://salesforce.stackexchange.com/questions/319277/how-to-debug-which-process-is-slowing-my-updates explains how to debug this issue using developer console


Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
This was selected as the best answer
SN SFSN SF

Hi Swetha ,

Thank you !

Regards,

Smita Naikar