You need to sign in to do that
Don't have an account?
Context Handler Methods
what is Context-Specific Handler Methods in triggers?
can any one explain wid example please>?
can any one explain wid example please>?
You need to sign in to do that
Don't have an account?
Hi Zach,
It is always a best practice to write a single trigger on object and process all operation in apex class (we called it as handler) instead of
writing logic in trigger. In handler you can specify the order of event based on different context variables. If you write many triggers in on object,
then you cannot control the order in which triggers gets executed.
For best practice, use below suggestion:
1. Pass all trigger context variables to static method (say "OperationManager")in handler class.
2. Create different static methods for different operations which you need to perform.
3. Control the order of different operation in "OperationManager" static method based on context variables.
4. Also you can create different apex classes and call them from "OperationManager" static method.
1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
2) https://developer.salesforce.com/page/Apex_Code_Best_Practices
1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts
2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.
3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers
Please check below example of Context-Specific Handler . In that we can add all logic inside the action class and method handling in handler class.
Example 1:-
Create one Trigger "AccountTrigger" Create one Trigger Handler Class
Create one Trigger Action Class
Context-Specific Handler Methods
One best-practice that I have picked up is to create context-specific handler methods in my Trigger handlers. In the above example, you’ll see that I’ve created a specific handler method just for after insert. If I were to implement new logic that ran on after update, I’d simply add a new handler method for it. Again, this handler method would be in the handler class, and not the Trigger. In this case, I might add some very light routing logic into the Trigger itself just so that the correct handler method is invoked: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
Let us know if this will help you
Great example thanks for posting! I get the AccountTrigger (logic less) calls the Handler class. Two thins that are unclear:
1. The trigger Action Class
2. How to work with handler methods that work with Map and Set collection types (e.g. before update)
This is what I got so far. I'm reviewing your article http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
to see if I can maybe get one of your methods into my code to test.
Logic less Trigger
Handler Class