Skip to content

Instantly share code, notes, and snippets.

@omniphx
Last active January 10, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omniphx/3865ba9eff05ccdfbd55ae1a87cd2d8c to your computer and use it in GitHub Desktop.
Save omniphx/3865ba9eff05ccdfbd55ae1a87cd2d8c to your computer and use it in GitHub Desktop.
public virtual class TriggerHandler {
public static Boolean shouldRun = true;
public static Boolean shouldHandlersRun = true;
public TriggerContext context;
private Boolean isTriggerExecuting;
public static void disable() {
TriggerHandler.shouldRun = false;
}
public static void enable() {
TriggerHandler.shouldRun = true;
}
public enum TriggerContext {
BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,
AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, AFTER_UNDELETE
}
protected TriggerHandler() {
this.isTriggerExecuting = Trigger.isExecuting;
if(!this.isTriggerExecuting) return;
else if(Trigger.isBefore && Trigger.isInsert) this.context = TriggerContext.BEFORE_INSERT;
else if(Trigger.isBefore && Trigger.isUpdate) this.context = TriggerContext.BEFORE_UPDATE;
else if(Trigger.isBefore && Trigger.isDelete) this.context = TriggerContext.BEFORE_DELETE;
else if(Trigger.isAfter && Trigger.isInsert) this.context = TriggerContext.AFTER_INSERT;
else if(Trigger.isAfter && Trigger.isUpdate) this.context = TriggerContext.AFTER_UPDATE;
else if(Trigger.isAfter && Trigger.isDelete) this.context = TriggerContext.AFTER_DELETE;
else if(Trigger.isAfter && Trigger.isUndelete) this.context = TriggerContext.AFTER_UNDELETE;
}
public void execute() {
if(!TriggerHandler.shouldRun) return;
this.executeHandlers();
}
public void executeHandlers() {
if(this.context == TriggerContext.BEFORE_INSERT) this.beforeInsert(Trigger.new);
else if(this.context == TriggerContext.BEFORE_UPDATE) this.beforeUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap);
else if(this.context == TriggerContext.BEFORE_DELETE) this.beforeDelete(Trigger.old, Trigger.oldMap);
else if(this.context == TriggerContext.AFTER_INSERT) this.afterInsert(Trigger.new, Trigger.newMap);
else if(this.context == TriggerContext.AFTER_UPDATE) this.afterUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap);
else if(this.context == TriggerContext.AFTER_DELETE) this.afterDelete(Trigger.old, Trigger.oldMap);
else if(this.context == TriggerContext.AFTER_UNDELETE) this.afterUndelete(Trigger.new, Trigger.newMap);
}
protected virtual void beforeInsert(List<SObject> newRecords) {}
protected virtual void beforeUpdate(List<SObject> updatedRecords, Map<Id, SObject> updatedRecordsMap, List<SObject> oldRecords, Map<Id, SObject> oldRecordsMap) {}
protected virtual void beforeDelete(List<SObject> deletedRecords, Map<Id, SObject> deletedRecordsMap) {}
protected virtual void afterInsert(List<SObject> newRecords, Map<Id, SObject> newRecordsMap) {}
protected virtual void afterUpdate(List<SObject> updatedRecords, Map<Id, SObject> updatedRecordsMap, List<SObject> oldRecords, Map<Id, SObject> oldRecordsMap) {}
protected virtual void afterDelete(List<SObject> deletedRecords, Map<Id, SObject> deletedRecordsMap) {}
protected virtual void afterUndelete(List<SObject> undeletedRecords, Map<Id, SObject> undeletedRecordsMap) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment