Aim :-Combining Multiple triggers on an object to a Single trigger:
This post is about he error's that arise when you have More than 1 trigger on any Object.
This post is about he error's that arise when you have More than 1 trigger on any Object.
Salesforce.com or force.com platform gives you a feasibility to write more than 1 trigger on any standard or custom object. which is a bad practice.
These Multiple triggers on an object which tend to Fire at specific DML operations or an events, may sometimes fire other triggers on the object and result in unnecessary data changes(Data corruption ) or recursive triggering or Show some wearied errors and break the business process.
Salesforce.Com says it's always a good practice to maintain a single trigger on any object. To meet different business logic on the the same object we might want to have different triggers on the object.
This can be solved by using simple techniques as shown below.
- Make your trigger to fire on a criteria.
- Try and create methods or apex classes where you can put all logic.Your trigger should initiate and process logic in methods.
- Make sure write code to meet governor limits.
Here is the sample how to write you trigger :
trigger TriggerName on ObjectName (after insert, before update,after update, before delete) { if(Trigger.isBefore) { if (Trigger.isUpdate) { CheckAndDeleteOldRecords(); } if(Trigger.isDelete) { DeleteRelatedCases(); } } if(trigger.isAfter) { if(Trigger.isInsert) { MigrateContacts(); } if (Trigger.isInsert || Trigger.isUpdate) { CheckAndCreateCasesAndTasks(); if(Trigger.isUpdate) { UpdateYearSummaryWithStatus(); UpdateStatus(); ApprovalSubmitterForCase(); } } } /// End of trigger
/// These methods can be in the same trigger but should start after the trigger.
private void UpdateStatus()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void CheckAndDeleteOldRecords()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void ApprovalSubmitterForCase()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void UpdateYearSummaryWithStatus()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void UpdateStatus()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void CheckAndDeleteOldRecords()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void ApprovalSubmitterForCase()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
private void UpdateYearSummaryWithStatus()
{
for(Contact c :trigger.new)
{
// Do your code here.
}
}
So On...This will reduce the risk and unwanted errors.Hoping this will help you.Happy Coding.
No comments:
Post a Comment