We have a scheduled job on Contacts that runs once a week. We have a couple processes that are also on Contacts. We are in the process of transitioning Workflow Rules into our Processes. Some there have been some edits lately.

Everything in Production has been tested in Sandbox, and thus far, have not had any problems. We arrived this morning to see that we had a handful of fails on that weekly job with the error:

“Contact: Every time Created or Edited” process failed. Give your Salesforce admin these details. This error occurred when the flow tried to update records: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: ContactBeforeInsertBeforeUpdate: System.LimitException: Apex CPU time limit exceeded

I cannot find the issue! I backtracked through the edits, manually trying to find which one could be causing a problem. And most recently, I manually updated a couple records with the changes that would be made in the import. No errors were triggered.

To make things even more odd, when I started to go though the fail errors one by one, it seemed that they had actually been updated to the correct information.

Any ideas or help on this would be greatly appreciated!

1 Answer 1


"Apex CPU time limit exceeded" doesn't imply a bug, but that your code/process might not be optimized.

Everything in Production has been tested in Sandbox, and thus far, have not had any problems.

Usually a big difference between Production and Sandbox org is the amout of data, such as object records, attachments, and metadata. If you have a Full Copy sandbox, you could use it to run performance testing.

Once you have a log with that System.LimitException (I suggest to set "Workflow", "Apex Code" and "Apex Profiling" at Finest level) from Developer Console you could find out what consumes the most CPU Time by switching to Analysis oerspective: click on "Debug" then on "Switch Perspective" and select "Analysis".
Open "Timeline" tab in "Execution overview" panel and you'll see what consume the most CPU Time.

There are several best practices you could look at, but there is no guarantee that these would be enough:

  1. Bulkify your code.
  2. Double check nested loop in your code.
  3. Use aggregate queries if possible: the database time is not calculated in CPU time.
  4. If possible, use WHERE clause to filter records instead of nesting ad if in a loop. Same as above: the database time is not calculated in CPU time.
  5. Move same-record field updates from Workflow Rules and Process Builder to before apex trigger (if any) or before-save Flow triggers. Keep in mind that if a record was updated by a workflow field updates (or a process), before and after triggers are executed one more time. A good reading.
  6. Check with your business if there is any process that could be asynchronous.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .