0

I have a bunch of triggers in the opportunity object which throw System.LimitException: Apex CPU time limit exceeded when we do a mass update on opportunities. In my client org, the client has created an opportunity hierarchy by creating a lookup field to an opp and there is a query which queries to find the parent and does some calculations on the parent opportunity. I tried to do a mass update using data loader and i get an error which is the cpu time limit exceeded error. Is there an easy way to debug this on the debug log which can point me to the line of code or could you guys provide some pointers on when this error will happen on triggers? Buyan

  • Actually the higher you crank debug log settings, the sooner in your execution you'll hit this error. The converse is also true. If you had all levels on FINEST, there is a modest chance you could alleviate the error by setting them all to NONE. – Adrian Larson Jul 19 '16 at 12:02
1

You can use Limits.getCPUTime() from the Limits Class to monitor the execution time.

That's tricky, though. CPU time depends on how long you code is running wich means that it depends on how many records are being inserted and the debug levels that you're using. It's hard to pinpoint a location.

To avoid hitting those limits, you can try lowering the debug levels to NONE or ERROR (Adrian's suggestion) or reducing the chunk of records being updated.

If that's something to be expected, you can use the Limits class to split your code or something. Like say, if you reach 80% of the limit, instead of processing other records, just save the ids so you can make another call. Or pass them to a future method.

| improve this answer | |
  • 3
    Calls to Limits.getCPUTime() are also troublesome because they consume CPU Time. – Adrian Larson Jul 19 '16 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.