Salesforce Certified Application Architect

Monday 30 December 2013

Apex Trigger Tip: Using One Trigger Per Object To Control Logic.

 How you can control the order of execution on trigger logic when you have multiple Apex triggers operating on the same object and event.  So if you have “UpdateAccountWithCase” and “UpdateAccountAfterOpportunity” as two examples, how do you know your Account will be updated with case related information before the opportunity data?
The answer is you don’t – at least not if you’ve got triggers separated out into multiple classes for a single object.  For this reason, and a few others, I’ve seen many companies utilize one trigger per data object that simply controls the flow of logic while calling the execution in other classes.  So instead of:
trigger UpdateAccountWithCase on Account (before insert, before update) {
/* execution logic */
}
trigger UpdateAccountAfterOpportunity on Account (before insert, before update) {
/* execution logic */
}
You would have one trigger, which examines the incoming event and hands off the relevant data to other classes:
trigger AccountTrigger (before insert, before update) {
if(Trigger.isBefore && Trigger.isInsert) {
UpdateAccountWithCase.handleAccount(Trigger.new);
UpdateAccountAfterOpportunity.handleAccount(Trigger.new);
}
}

No comments:

Post a Comment

Salesforce Certified Application Architect & Certified Data Architecture and Management Designer Exam

How to pass Salesforce Certified Data Architecture and Management Designer Exam This exam was 1st architect exam for me. Its not that muc...