Weekly about 10,000 records are inserted and there is a Trigger that calls a class to process these records. All this process is synchronous, but for some cases it's taking too long to process and getting CPU time limit error. In that case I have to change the class for a future method or batch? I don't know how to handle that in a asynchronous way since I have to get just the records that are inserted.

  • 1
    Please edit your question to include some snippet of your apex trigger/class. – Hengky Ilawan Jul 15 at 2:40
  • If you have any Process Builders related to those records, be aware that they eat up cpu time. – Mike Lockett Jul 15 at 3:18

Batchable is not appropriate for use in triggers because of the maximum queue size. Use either future or Queueable.

| improve this answer | |

Find out what consumes the most CPU Time

In Developer Console ==> Select Debug on the top menu -> Switch Perspective -> Analysis (Predefined)

Choose Timeline ==> Scale (Choose the one fits you).

On this timeline view above, we can now see that Apex, Workflow, and Database actions are executed in sequence and how much time each of them consumes.

  • One of the common reasons we hit the CPU limit is that the trigger logic enters unexpectedly multiple times. Trigger logic finishes first, workflow follows to update the record which then causes the trigger logic to re-enter.

  • Another reason is the trigger custom code logic itself. Is your Apex code performant? For example, nested loops can be very slow when processing a large volume of records.

You Can also check the API REQUESTS, LAST 24 HOURS under System Overview

Setup --> Environments --> System Overview

We can see the API Usages for last 24 Hours.

| improve this answer | |

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.