I've read through Nuances of Working with Tidy Trigger Pattern, but I'm still struggling to understand something:

For what portion of the life of the "execution context" does an attribute of the instantiated trigger handler keep its values?


  • If the answer is, "also the entire execution context," does it matter whether I make the "trigger helper" class's attributes static vs. instance-level?

  • If the answer is, "not the entire execution context," but I'd like some data that my methods work with to live that long (e.g. recursion-prevention ID sets) ... Can I just point the "trigger helper's" "Trigger.new-looping" instance methods to read from + write to static attributes, and expect everything to behave about the same as it does when writing "helper classes" that loop using static methods + static attributes?

1 Answer 1


The instantiated trigger handler, as intended in the original post, lasts only as long as the context that spawned it. For example, in a Visualforce page, the handler will not exist before or after a DML statement. As such, it does not last the entire execution context. If you need to deal with recursion, you can indeed use static variables and/or methods as you need in order to deal with recursion. There's no unusual surprises you need to be worried about compared to other types of recursion control.

  • So ... if an AfterUpdate "tidy" trigger on Contact spawns DML that results in an Update to one of the Contact records processed ... would that "2nd pass" be using the same instance of "TidyContactTriggerHandler" that the "1st pass" used or a new one?
    – k..
    Sep 14, 2017 at 19:58
  • @k.. it'd be a new instance. Static variables would have persisted, but local variables will not.
    – sfdcfox
    Sep 14, 2017 at 20:05
  • Interesting -- that makes me wonder exactly what, besides visually "tidy" code, is so desirable about this particular flavor of "just 1 trigger & handler per object" -- it seems that it adds CPU overhead for instantiating things that didn't need to be instantiated?
    – k..
    Sep 15, 2017 at 14:36
  • @k.. There are benefits and drawbacks to using both static and instantiated trigger handlers. Notably, static handlers require extra care to not leak per-trigger data across recursive instances. I'm not against instantiated handlers if it makes sense, but I've seen too many examples where they do just that.
    – sfdcfox
    Sep 15, 2017 at 15:23
  • Things I learned after completely rewriting a "tidy" trigger my old way: make your recursion-prevention variable static class-level, but do NOT do this with "toInsert"/"toUpdate" collection variables ... speaking of "old data that shouldn't leak into the next pass of the trigger." Sigh. (Note to self: In a static helper, make them method-called-by-trigger-scope variables so they initialize to empty every pass. In a "tidy" helper, make them instance variables because instantiation is what's called by the trigger. The code in the trigger is your "for-loop" through unique "trigger passes.")
    – k..
    Sep 15, 2017 at 21:32

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.