- I started developing on force.com, I used bare bone approach as mentioned in Apex developer guide.
- After sometime I was using something similar to what Steve mentioned in his blog, I was happy to see that Steve suggested a similar practice.
- Eventually tried something that is similar to what Mike suggested in his blog.
After trying all of the above trigger templates, I was still not happy when working solo or when working with a big team of engineers on the same project. I wanted something simple to adopt easily with flexibility to extend/add as required.
So, I started working on a new trigger templates with these motives in mind
- Framework/Base template code to start writing a new trigger should be minimal or none.
- It should allow any complex and reusable trigger code to be written.
- It should allow easy fixes other commonly visible problems, like trigger firing twice, as explained by Ray Dehler.
- Optionally create handlers with @future support.
- Ability to easily order and add multiple handlers on a single trigger event.
Yet Another Trigger Template !
“Triggers.cls” is the new Trigger template. This class doesn’t needs to be copied per sobject trigger and will be reused throughout the org. Here is the code snippet for it
Using the template
We will again follow the same one trigger per Sobject rule and just write handlers for each event like before insert, after update etc. Handlers can be created by implementing interface “Triggers.Handler”, only contract of this interface is to write all the trigger logic using the Trigger context variables as and when required. Here are a few sample handlers(these handlers can be nested into one big class to avoid too many classes)
Now the actual Task trigger will be as follows
Looking forward for your views and thoughts on the same. I’m sure it’s a good topic to discuss and debate on.