Apex Trigger Framework in Salesforce with best practise
Trigger framework is not just needed for having single object per trigger guidance but also helps in different other reasons.
According to the trigger framework :
1) Create a single trigger for each object.
2) A handler class which will call Action or Apex Interface based Trigger Framework Implementation
2) A handler class which will call Action or Apex Interface based Trigger Framework Implementation
- One Trigger -One Object Policy
- Framework Implementation
- Bulkified Code
- Avoid SOQL Queries or DML statements inside FOR Loops
- Using Collections, Streamlining Queries, and Efficient For Loops
- No Hardcoding IDs
Types of Framework available :
1.Handler/Controller Framework
2.Lightweight Apex Trigger Framework (Source: http://chrisaldridge.com/triggers/lightweight-apex-trigger-framework/)
Handler Framework Example:
trigger ABCTrigger on Account( after insert, after update, before insert, before update)
{
ABCHandler handlerFM = new ABCHandler(Trigger.isExecuting, Trigger.size);
if( Trigger.isInsert )
{
if(Trigger.isBefore)
{
handlerFM .OnBeforeInsert(trigger.New);
}
else if (Trigger.isAfter)
{
handlerFM .OnAfterInsert(trigger.New);
}
}
else if ( Trigger.isUpdate )
{
if(Trigger.isBefore)
{
handlerFM .OnBeforeUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
}
else if (Trigger.isAfter)
{
handlerFM .OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
}
}
}
Lightweight Apex Trigger Framework
This is what I think is the best implementation of the trigger framework. Just check the URL for complete implementation (http://chrisaldridge.com/triggers/lightweight-apex-trigger-framework/)
Source includes: Salesforce.com /Chris Aldaridges blog.
Comments
Post a Comment