Apex CPU Time Limit Exceeded Errors in Salesforce
Issue:
Apex CPU time limit exceeded errors occur when an Apex transaction exceeds the 10 second CPU time limit. This can happen for a variety of reasons, such as inefficient code, nested loops, or multiple triggers.
Symptoms:
When an Apex CPU time limit exceeded error occurs, you will see the following error message:
Apex CPU time limit exceeded. An unexpected error has occurred. Your solution provider has been notified (Advendio).
(Please note that Advendio does not directly receive these error notifications. Instead, it is typically the end user who is alerted. The mention of the “solution provider” cannot be omitted, as it's a standard Salesforce message for non-native features. If this is a front end process then the error is received directly in the UI, if its a back end process the error is received in a backend job message)
Causes:
There are many reasons why an Apex CPU time limit exceeded error can occur. Some of the most common causes include:
If your Apex code is inefficient, it can take longer to execute and exceed the CPU time limit.
Nested loops can also increase the amount of CPU time used by your Apex code.
If multiple triggers are fired on a single update, this can also increase the amount of CPU time used.
Resolution:
There are a number of things you can do to resolve Apex CPU time limit exceeded errors. Some of the most common resolutions include:
Make sure your Apex code is efficient and free of unnecessary loops or recursion.
If possible, try to reduce the number of triggers that are fired on a single update.
A profiler can help you identify areas of your code that are using excessive CPU time.
Prevention:
The best way to prevent Apex CPU time limit exceeded errors is to avoid the causes listed above. Here are some tips for preventing these errors:
Make sure your Apex code is efficient and free of unnecessary loops or recursion.
If possible, try to reduce the number of triggers that are fired on a single update.
A profiler can help you identify areas of your code that are using excessive CPU time.