Apex CPU Time Limit Exceeded Error - Explainer

Last published at: 2024-08-01 08:15:41 UTC

leadassist.assignLead: System.LimitException: Apex CPU time limit exceeded.

You might see this error and naturally think it is something going wrong in SuperRoundRobin.

First let's talk about how Salesforce calculate how 'Apex CPU time' is consumed. All computational processes consume CPU and because Salesforce is a multi-tenant system it imposes limits on things like CPU usage to prevent runaway processes from hogging too much resource (and slowing down other users/orgs).

The Apex CPU time 'stopwatch' is started at the beginning of the transaction, when the first bit of Apex is encountered. In any transaction where SuperRoundRobin is involved, SuperRoundRobin is often 'blamed' because it is one of the first bits of Apex to run; SuperRoundRobin starts the stopwatch.

As the transaction proceeds, any other automated process (Triggers, Flow, Process Builder, Workflow, Validation etc) will also consume Apex CPU time, and contributes to the 10 second time limit.

So even though SuperRoundRobin started the stopwatch, other processes will contribute to the 10 second limit and when 10 seconds is reached, the error will appear to cite SuperRoundRobin as the cause.

This isn't an excuse - SuperRoundRobin will certainly have contributed to the 10 seconds of Apex CPU time used. The key is to understand the whole transaction and what other processes have contributed to the timeout occurring, and then optimising the relevant pieces of automation.

So what to do?

  • Any batch processing will amplify the effects of resource intensive automation. Try reducing the batch size. If a record takes 60ms to process, and you are importing a batch of 200, then that will likely trip the time limit.
  • Capture debug logs that contain long running transactions and analyse them to understand what is contributing to the resource consumption. We can help you capture and analyse these logs.
  • Look at other triggers, Flows and Process Builders to see what their involvement is in the transaction. Temporarily disable them if you can to see what effect that has on your transactions.