3

Testing an apex class and decided to do some bulk testing by inserting 4,000 Account records. However i am getting a CPU timeout error (over 10 second execution time) and a portion of this error seems to be the time it is taking between the before Insert Trigger and the After Insert trigger (around a second, you can see it jump from 16 seconds to 18 seconds).

Bulk Load

is this standard or is something abnormal with my org? I know that between before and after triggers these steps take place:

Runs most system validation steps again and execute duplicate rules.

Its not even getting into the method i was trying to test (so that isnt the issue), rather its timing out before that. I know i could go async but this was intended more as a stress test of the org. Does anyone have any thoughts on what could be causing the 1 second gap execution?

6
  • 1
    Is it the finest debug log? Usually it also shows the database time Dec 31 '19 at 19:35
  • Have you looked at the trigger lifecycle ("order of execution") documentation to see if you have some flow or process builder, or something else, that is causing this delay? I would also say that writing a "stress test" is basically pointless in the Salesforce ecosystem.
    – Phil W
    Dec 31 '19 at 19:39
  • @philW i have and mentioned so in my question. Neither flows or process builders take place between before triggers and after triggers. And i disagree, i would like to maintain the ability to insert 4k accounts into the org if requirements call for it
    – Bobbygllh
    Dec 31 '19 at 19:57
  • @pranay yes, i do have the finest debug level for DB on
    – Bobbygllh
    Dec 31 '19 at 20:03
  • Do you have a lot of validation rules or duplicate rules set up? Dec 31 '19 at 20:10
5

Database time does not count towards the CPU time, even when triggers are called recursively. The database time here covers standard validations (required fields, duplicate rules, etc), as well as other steps, such as calculating formulas and reloading the record from the database. You'll need to do some profiling to figure out where the extra time is going. Note that Processes and Flows count towards CPU time, so if you have complicated flows, they may be actually causing the problem.

3
  • i am more wondering about the missing second between before and after triggers, that seems really long, right? and only workflows and they dont seem to be causing the issue, 99.7% apex time allocation, 2.7% workflows
    – Bobbygllh
    Dec 31 '19 at 20:42
  • Is it consistently like this, or could it be variable? (Remember that you're on a multi-tenant environment and your org will run when there's resource available.)
    – Phil W
    Dec 31 '19 at 21:09
  • @Bobbygllh It's not "workflow" time, it's just database time. I've seen orgs that consistently have 2-4 seconds of time like this that would spike even as high as 10 seconds. It depends on database contention (locks), validation rules, number of fields and records involved, debug levels (max debug can easily increase DB time by 300%). This isn't necessarily unusual.
    – sfdcfox
    Dec 31 '19 at 22:09

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.