April 26, 2017 by LanceShi
6.1 Framework, why?
In Chapter 3 and 4 we have introduced how to write a trigger. And in Lecture 4.2 we have introduced TriggerHandler class.
In most Salesforce orgs and implementations, I would highly suggest you using a TriggerHandler framework. I hear what you say, why bother? Isn’t the TriggerHandler class we have introduced in 4.2 good enough?
Well, for tiny projects, it might be good enough. But for any enterprise structure, even though we can still use it, it is much better to apply a TriggerHandler framework. That’s because for most triggers, we have a bunch of similar requirements:
- We need to run the different functions in a chronicle order: before insert, before update, after insert, after update, before delete.
- We need to get rid of infinite loops.
- We need to provide a way to reset the infinite loop counter. So in case of update contact1; update contact2; We can ensure the trigger runs for both SObjects.
- We need to provide a method to overpass the trigger running so that some legacy data can still be pushed through.
- And other things (like logging, limit tracking, etc.). You can also add them into your TriggerHandler class.
For all those reasons, it is highly recommended here that we should have a base TriggerHandler class and let all the TriggerHandlerSObject classes inherit from that base class.
And that, is our TriggerHandler framework.
Remember, the TriggerHandler class is here to help us make things easier, not making it harder. And it is not hard to apply it.