Some standards to follow while writing Triggers:
a) Write only 1 trigger per object to handle all scenarios
b) Trigger should not hold any logic--bulk of the logic should be done outside of the core trigger code
in a seperate class
c) If your trigger has an after update then make sure that you are covering for recursive trigger traps
by using a boolean variable at the start of the execution
Write your trigger with the most basic of codes:
trigger OpportunityTrigger on Opportunity (after insert, after update) {
if(Trigger.isAfter && Trigger.isInsert) {
OpportunityTriggerHandler.handleAfterInsert(Trigger.new);
} else if(Trigger.isAfter && Trigger.isUpdate) {
OpportunityTriggerHandler.handleAfterInsert(Trigger.new, Trigger.old);
}
}
a) Write only 1 trigger per object to handle all scenarios
b) Trigger should not hold any logic--bulk of the logic should be done outside of the core trigger code
in a seperate class
c) If your trigger has an after update then make sure that you are covering for recursive trigger traps
by using a boolean variable at the start of the execution
Write your trigger with the most basic of codes:
trigger OpportunityTrigger on Opportunity (after insert, after update) {
if(Trigger.isAfter && Trigger.isInsert) {
OpportunityTriggerHandler.handleAfterInsert(Trigger.new);
} else if(Trigger.isAfter && Trigger.isUpdate) {
OpportunityTriggerHandler.handleAfterInsert(Trigger.new, Trigger.old);
}
}
Do all the handling in
public class OpportunityTriggerHandler {
public static void handleAfterInsert(List opps) {
// handler logic
}
}
Over time as your logic increases it will be a lot readable and maintainable.
No comments:
Post a Comment