Thursday, August 12, 2021

Analysis on CPU Timeout Exception

We all might have seen at-least once the exception 'Apex CPU time limit exceeded'.  If transactions consume too much CPU time, they will be shut down and for any synchronous transaction, Salesforce limits the amount of CPU time to 10 seconds.  Have to admit that It is very hard to find the root-cause for this as there could be multiple components that gets executed in a transaction and any component could be the reason for it. 

In this post, let us understand the procedure to figure-out the way of finding the time consumed by each component in a transaction.  I am writing a simple WFR on Account object to showcase the demonstration.

Below is the WFR written


Below is the Field Update associated


To identify the time consumed, let's make sure that the debug logs are enabled for the User.   Lets insert a contact record for an existing account and capture the time.  For capturing the time, let me walk you through the process.  
  1. Perform the action.  i.e., "Inserting contact" in this scenario
  2. Open Developer Console.  Click on "Logs" tab at the bottom left and open the corresponding debug log pertaining to the action made.
  3. Now click on Debug > Switch Perspective > Analysis (Predefined).  Refer to the below image for better understanding.

  4. Now different panels will appear and under "Slack Tree" section click on "Performance Tree".  Refer to the below image.   All the component types that get executed in the transaction will be shown here with the time and heap consumed.  

  5. As I do not have any other event-centric component, only WFR is coming up.
  6. As you can see, it will show the time taken / heap consumed by each component if present.  
This will help you to identify the component that consumed more time and then you can make change accordingly to fix the issue

..Bazingaa..

5 comments: