Created
October 23, 2020 08:00
-
-
Save swapnilshrikhande/98086e40c9a428f950e4792ef4ea00b1 to your computer and use it in GitHub Desktop.
Generic trigger
This file contains hidden or 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 virtual class GenericTrigger { | |
| public SObject[] recordOldList; | |
| public SObject[] recordNewList; | |
| public Map<Id, SObject> recordOldListMap; | |
| public GenericTrigger(SObject[] recordOldList, SObject[] recordNewList) { | |
| this.recordNewList = recordNewList == null ? new SObject[] {} : recordNewList.clone(); | |
| this.recordOldList = recordOldList == null ? new SObject[] {} : recordOldList.clone(); | |
| this.recordOldListMap = new Map<Id, SObject> {}; | |
| // Build the old map | |
| for(SObject recordOld : this.recordOldList) { | |
| if (recordOld.Id != null) { | |
| recordOldListMap.put(recordOld.Id, recordOld); | |
| } | |
| } | |
| } | |
| public virtual Boolean executable(SObject recordOld, SObject recordNew) { | |
| return true; | |
| } | |
| public virtual Boolean isDisabled() { | |
| //@TODO add support for Feature Config to enable disabling individual trigger handlers based on the class name | |
| return false; | |
| } | |
| public virtual void execute() { | |
| if (isDisabled() == true || recordNewList.isEmpty() ) { | |
| return; | |
| } | |
| SObject[] recordUpdateableList = new SObject[] {}; | |
| SObjectType recordType = recordNewList[0].Id.getSobjectType(); | |
| for(SObject recordNew : recordNewList) { | |
| SObject recordOld = recordOldListMap.get(recordNew.Id); | |
| recordOld = recordOld == null ? recordType.newSObject() : recordOld; | |
| if (executable(recordOld, recordNew)) { | |
| recordUpdateableList.add(recordNew); | |
| } | |
| } | |
| if (recordUpdateableList.size() != 0) { | |
| execute(recordUpdateableList, trigger.IsAfter); | |
| } | |
| } | |
| public virtual void execute(SObject[] recordList, Boolean forceUpdate) { | |
| // do nothing here | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment