Last active
March 19, 2019 17:08
-
-
Save jongpie/0f02dac6debad96eef3ad81635ce9abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger AccountTrigger on Account(before insert, before update, before delete, after insert, after update, after delete, after undelete) { | |
new AccountTriggerHandler.execute(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountTriggerHandler extends TriggerHandler { | |
protected override void executeBeforeInsert(List<Sobject> newRecords) { | |
List<Account> newAccounts = (List<Account>)newRecords; | |
for(Account account : newAccounts) { | |
// Call some private methods here for each record | |
this.runSomeMethod(account); | |
} | |
// Or you can call methods that accepts a list instead | |
this.runSomeBulkMethod(newAccounts); | |
} | |
private void runSomeMethod(Account account) { | |
// Add some logic here | |
} | |
private void runSomeBulkMethod(List<Account> accounts) { | |
// Add some logic here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************************************** | |
* This file is part of the Simple Trigger handler project, released under the MIT License. * | |
* See LICENSE file or go to https://github.com/jongpie/SimpleTriggerHandler for full license details. * | |
******************************************************************************************************/ | |
public abstract class TriggerHandler { | |
public void execute() { | |
System.debug('Starting handler execution. Current class is: ' + this.getClassName()); | |
// Dont execute if custom setting has disabled triggers | |
if(this.isDisabled()) return; | |
switch on Trigger.operationType { | |
when BEFORE_INSERT { this.executeBeforeInsert(Trigger.new); } | |
when BEFORE_UPDATE { this.executeBeforeUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap); } | |
when BEFORE_DELETE { this.executeBeforeDelete(Trigger.old, Trigger.oldMap); } | |
when AFTER_INSERT { this.executeAfterInsert(Trigger.new, Trigger.newMap); } | |
when AFTER_UPDATE { this.executeAfterUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap); } | |
when AFTER_DELETE { this.executeAfterDelete(Trigger.old, Trigger.oldMap); } | |
when AFTER_UNDELETE { this.executeAfterUndelete(Trigger.new, Trigger.newMap); } | |
} | |
} | |
protected virtual void executeBeforeInsert(List<Sobject> newRecords) {} | |
protected virtual void executeBeforeUpdate(List<Sobject> updatedRecords, Map<Id, Sobject> updatedRecordsById, List<Sobject> oldRecords, Map<Id, Sobject> oldRecordsById) {} | |
protected virtual void executeBeforeDelete(List<Sobject> deletedRecords, Map<Id, Sobject> deletedRecordsById) {} | |
protected virtual void executeAfterInsert(List<Sobject> newRecords, Map<Id, Sobject> newRecordsById) {} | |
protected virtual void executeAfterUpdate(List<Sobject> updatedRecords, Map<Id, Sobject> updatedRecordsById, List<Sobject> oldRecords, Map<Id, Sobject> oldRecordsById) {} | |
protected virtual void executeAfterDelete(List<Sobject> deletedRecords, Map<Id, Sobject> deletedRecordsById) {} | |
protected virtual void executeAfterUndelete(List<Sobject> undeletedRecords, Map<Id, Sobject> undeletedRecordsById) {} | |
private String getClassName() { | |
return String.valueOf(this).split(':')[0]; | |
} | |
private Boolean isDisabled() { | |
// Add your logic to read your custom setting here | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment