Apex Trigger Framework v2.1
Apex Trigger Framework v2.1
Apex Trigger Framework v2.1
Presented by
Joseph Msallem
Nicole Tannous
Ziad Azoury
1
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Plan
06 Demo
2
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Current Trigger Best Practice v2,0
(Best Practice v2,0) • We also had the PAD ByPass while verifying the DML. We can
also bypass by method.
3
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Current Trigger Best Practice v2,0
if(PAD.CanTrigger('AccountBeforeInsert’)){
if(PAD.CanTrigger('AccountBeforeInsertMethode01’)){
AP01Account.myMethodeAccBeforeInsert01(Trigger.new, Trigger.old);
}
if(PAD.CanTrigger('AccountBeforeInsertMethode02’)){
AP01Account.myMethodeAccBeforeInsert02(Trigger.new, Trigger.old);
}
}
}
if(PAD.CanTrigger('AccountAfterInsert’)){
if(PAD.CanTrigger('AccountAfterInsertMethode01’)){
AP01Account.myMethodeAccAfterInsert01(Trigger.new, Trigger.old);
}
}
}
if(PAD.CanTrigger('AccountBeforeUpdate’)){
if(PAD.CanTrigger('AccountBeforeUpdateMethode01’)){
AP01Account.myMethodeAccBeforeUpdate01(Trigger.new, Trigger.old);
}
}
}
4
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Current Trigger Best Practice v3,0
5
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Current Trigger Best Practice v3,0
trigger AccountTrigger on Account (before insert, before update, before delete, after insert, after update){
if(Trigger.isBefore){
if(Trigger.isInsert && PAD.CanTrigger('AccountBeforeInsert’)){
ATC.myMethodeBeforeInsert(Trigger.new);
}
if(Trigger.isUpdate && PAD.CanTrigger('AccountBeforeUpdate')){
if(PAD.CanTrigger('AccountBeforeUpdateMethode01'))
ATC.myMethodeBeforeUpdate01(Trigger.new, Trigger.old);
if(PAD.CanTrigger('AccountBeforeUpdateMethode02'))
ATC.myMethodeBeforeUpdate02(Trigger.new);
}
if(Trigger.isDelete && PAD.CanTrigger('AccountBeforeDelete')){
ATC.myMethodeBeforeDelete(Trigger.new);
}
}
if(Trigger.isAfter){
if(Trigger.isInsert && PAD.CanTrigger('AccountAfterInsert')){
if(PAD.CanTrigger('AccountAfterInsertMethode01'))
ATC.myMethodeAfterInsert01(Trigger.new, Trigger.old);
if(PAD.CanTrigger('AccountAfterInsertMethode02'))
ATC.myMethodeAfterInsert02(Trigger.new);
}
if(Trigger.isUpdate && PAD.CanTrigger('AccountAfterUpdate')){
ATC.myMethodeAfterUpdate(Trigger.new);
}
}
}
6
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Definition
7
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Apex Trigger Framework Architecture
SObject Interface
ITriggerHandler
<sObject>Trigger Implements
Calls
Calls Dynamic Binding
<sObject>TriggerHandler
TriggerDispatcher
Run (ITriggerHandler handler)
9
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Steps to create it
01 Each trigger must be implemented in a custom metadata that allows the trigger to be
active/inactive from UI.
10
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Steps to create it
02 Lorem Ipsum
03 Lorem Ipsum
04 Lorem Ipsum
11
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Steps to create it
void beforeInsert();
02
void afterInsert();
void beforeUpdate();
Lorem Ipsum
void afterUpdate();
void beforeDelete();
void afterDelete();
void afterUnDelete();
Boolean isDisabled();
03
}
Lorem Ipsum
04 Lorem Ipsum
12
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Steps to create it
04 Lorem Ipsum
13
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Steps to create it
05 Create the SObject Trigger Handler extending the Trigger Handler and implementing the Trigger
Handler Interface
public SObjectNameTriggerHandler(){
this.newMap = (Map<Id, SObject>) Trigger.newMap;
this.oldMap = (Map<Id, SObject>) Trigger.oldMap;
this.triggerNew= (List<SObject>) Trigger.New;
this.triggerOld= (List<SObject>) Trigger.Old;
03
}
public static Boolean TriggerDisabled = false;
LoremBoolean
public IpsumisDisabled(){
return TriggerDisabled;
}
public override void beforeInsert() {}
04
public override void beforeUpdate() {}
Loremoverride
public Ipsum void afterUpdate() {}
public override void beforeDelete() {}
14
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Steps to create it
/*
@Author : TMA NTA
@CreatedDate : 11-02-2021
@Description : SObjectName Trigger.
*/
trigger SObjectNameTrigger on SObject(before insert, after insert, before update, after update, before delete, after
delete, after unDelete) {
new SObjectNameTriggerHandler().run();
}
15
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Benefits
16
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Cool features
17
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Cool features
Bypass API
What if you want to tell other trigger handlers to halt execution? That's easy with the bypass api:
public class OpportunityTriggerHandler extends TriggerHandler {
public override void afterUpdate() {
List<Opportunity> opps = [SELECT Id, AccountId FROM Opportunity WHERE Id IN :Trigger.newMap.keySet()];
Account acc = [SELECT Id, Name FROM Account WHERE Id = :opps.get(0).AccountId];
TriggerHandler.bypass('AccountTriggerHandler’);
acc.Name = 'No Trigger’;
update acc; // won't invoke the AccountTriggerHandler
TriggerHandler.clearBypass('AccountTriggerHandler’);
acc.Name = 'With Trigger’;
update acc; // will invoke the AccountTriggerHandler
}
}
If you want to clear all bypasses for the transaction, simple use the clearAllBypasses method, as in:
// ... done with bypasses!
TriggerHandler.clearAllBypasses();
18
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
Demo
DEMO
19
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
THANK YOU
ANY QUESTIONS?
20
Copyright © 2020 EI-Technologies-Lebanon SAL.
Apex Trigger Framework
References
https://github.com/kevinohara80/sfdc-trigger-framework
https://www.biswajeetsamal.com/blog/salesforce-apex-trigger-framework/
https://trailhead.salesforce.com/en/content/learn/modules/success-cloud-coding-conventions