Apex CPU time limit exceeded : Apex Trigger Error

2:40 PM



You might get this Error fairly frequently when developing complex Apex Triggers. In this article, we will look at the causes and possible solutions to address the issue.


What?

This error message typically means that the Apex Trigger is taking too long to execute. The maximum CPU time for a trigger is 10,000 milliseconds or 10 seconds. Note that the time taken for DML and SOQL operations does not fall into this category. 


Why?

When you get this error, it DOES NOT mean that you have too many DML operations or large SOQL queries. Rather, it means that there is a problem in the way you handle the result of those operations.

Things to check

  1. Try using a MAP instead of a LIST. A MAP is always much efficient to use.
  2. Try to avoid iterations or FOR loops wherever possible.
  3. Check your nested FOR Loops. This is one MAJOR area which consume too much time. Try to minimize the iterations.

1 comments

  1. There is no straight solution if the developer is hitting this error, basically need to go over the whole execution context and need to tweak the code where ever possible.

    ReplyDelete