In ApexHours we did one session on Apex Trigger Framework in Salesforce with ADAM OLSHANSKY. In that session we talk about what is a benefit of using a trigger framework, How many trigger framework are available, which one is lightweight apex trigger framework and Comparison of different approaches.
In that session we talk about all below Trigger Framework
1) Trigger Handler Pattern
2) Trigger Framework using a Virtual Class
3) Trigger Framework using an Interface
4) An architecture framework to handle triggers
Please check this recording for code walk-through. Accomplishments with this Trigger framework :-
Please check this recording for code walk-through. Accomplishments with this Trigger framework :-
Please check this recording for code walk-through. Accomplishments with this Trigger framework :-
Please check our YouTube Recording to learn more about all Trigger Framework and Code.
https://www.youtube.com/watch?v=U5ZOA9EY3Kg&t=7s
Thank
In that session we talk about all below Trigger Framework
1) Trigger Handler Pattern
2) Trigger Framework using a Virtual Class
3) Trigger Framework using an Interface
4) An architecture framework to handle triggers
Why Trigger Framework ?
A framework may, however, greatly simplify your development efforts when your code base gets large. In a nutshell, your framework should have the following goals:-- Generic code that can be extended for any object
- Ensures triggers are consistently handled and only required code is needed
- Allows for simple Triggers and Handlers
- Handles routing for you
- Enforces consistent trigger behavior
- Easily allows for trigger bypasses
Trigger Handler Pattern
Please check this post to learn about Handler pattern and code. Lets talk about what is the advantage of Trigger Handler Patter.- Apex Class that handles trigger logic
- Allows code to be called from other code or tests
- Uses specific Trigger contexts and Trigger variables for routing
- Keep Triggers Simple
- Allow for greater flexibility
- Make code reusable
- Unit tests are much easier
Please check this recording for code walk-through. What is missing in Handler pattern ?
- Still duplicated code in every trigger
- How can we simplify things?
- How can we make our process repeatable?
Trigger Framework using a Virtual Class
Please check this post to learn more about Virtual Class Trigger Framework.Please check this recording for code walk-through. Accomplishments with this Trigger framework :-
- 1 line trigger
- Only need to add handler methods that we want to use
- All routing is handled for us
Trigger Framework using an Interface
Please check this post to learn about this framework.Please check this recording for code walk-through. Accomplishments with this Trigger framework :-
- 1 line trigger
- All routing is handled for us
- All handlers are consistent and will have the same methods
- Multiple ways to deactivate a trigger
An architecture framework to handle triggers
Please check this post to learn about this framework.Please check this recording for code walk-through. Accomplishments with this Trigger framework :-
- 1 line trigger
- All routing is handled for us
- Establish all methods while letting us pick and choose which ones we want
- Individual event handler methods
Please check our YouTube Recording to learn more about all Trigger Framework and Code.
https://www.youtube.com/watch?v=U5ZOA9EY3Kg&t=7s
Here is link for all frameworks:-
- Trigger Frameworks and Apex Trigger Best Practices by Kevin O’Hara
- Trigger Pattern for Tidy, Streamlined, Bulkified Triggers by Tony Scott
- Hari Krishnan’s architecture framework
- Lightweight Apex Trigger Framework
Thank








1) isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or anexecuteanonymous() API call.
2) isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or theAPI.
3) isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or theAPI.
4) isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or theAPI.
5) isBefore Returns true if this trigger was fired before any record was saved.
6) isAfter Returns true if this trigger was fired after all records were saved.
7) isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
8) new Returns a list of the new versions of the sObject records.Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
9) newMap A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.
10) old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
11) oldMap A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.
12) size The total number of records in a trigger invocation, both old and new.
Sample trigger
trigger AccountTrigger on Account( after insert, after update, before insert, before update) { AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size); if( Trigger.isInsert ) { if(Trigger.isBefore) { handler.OnBeforeInsert(trigger.New); } else { handler.OnAfterInsert(trigger.New); } } else if ( Trigger.isUpdate ) { if(Trigger.isBefore) { handler.OnBeforeUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap); } else { handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap); } } }Create one Trigger Handler Class
Create one Trigger Action Class
public without sharing class AccountActions { public static void updateContact ( List<Account> newAccount) { // Add your logic here } }SourcePlease check below post for full example of Trigger
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
Please let us know if this will help you.