0

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.

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

2 Answers 2

1

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

1

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.

You must log in to answer this question.

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