3

I was hitting SOQL query limits in my Test class so I thought I'd test things out with some Limits.getQueries() bench-marking.

I replicated the use case that would fire the trigger on 1 Order and looked at the results. The class that handles the trigger starts with just looping through the Orders in the trigger (following best practice, this isn't in the trigger class itself, so it's passed the Order IDs and it's looping through them for some logic).

Anyway, in a loop that just goes through the IDs for some logic and no queries within it, the first pass around, it had 3 queries so far. I figured, ok, something beforehand maybe did that...but then I noticed the debug again, with a higher number. But this loop should only have one Order in it since that's all I updated.

So, I included the Order ID to see why it's grabbing other Orders...but the weird thing is, it's the same ID! The first time with 3, the next time with 17, and then a third time with 35 queries made.

Another bench-mark also happened three times: first with 2 queries, then with 16, then with 34. Clearly, this is happening three times as much as I expect for this one order.

What gives?

  • Could you include your trigger and test code? Do you have more than one trigger running on the same object, or any workflow rules or processes? – David Reed Jun 27 '18 at 22:19
4

I think I know why! I insert Order Items to the Order, and there's a trigger on Order Items that in some cases (this case) edits a part of its parent.

Hence the trigger again.

  • 2
    That's exactly right. You can end up triggering the same trigger multiple times if you have rollup summary fields (and Orders do automatically because Order Line Items update some fields on the Order). – sfdcfox Jun 28 '18 at 2:53
0

I am Jeff Du. Maybe below information can help you.

For trigger in salesforce,please follow the best pricatice in the Salesforce Apex document. To prevent the trigger to run many times, please take note to:

  1. Control the logic to fire the trigger , not to trigger it every time when the record is update, such as the field value is changed;
  2. Please consider to use the static variable to Control the run times.

Static variable:

A static variable is static only within the scope of the Apex transaction. It’s not static across the server or the entire organization. The value of a static variable persists within the context of a single transaction and is reset across transaction boundaries. For example, if an Apex DML request causes a trigger to fire multiple times, the static variables persist across these trigger invocations. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

How to avoid Recursive trigger

https://developer.salesforce.com/forums/?id=906F0000000DDdfIAG

Trigger best practice:

https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

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.