Created
March 7, 2021 06:10
-
-
Save LawrenceLoz/95445c8d6811e7ddb99963023415d701 to your computer and use it in GitHub Desktop.
Trigger and handler class examples - Nebula Core
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 with sharing class AccountHandlerSetActivityDateOnSelf implements BeforeInsert, BeforeUpdate { | |
public void handleBeforeInsert(List<Account> newList) { | |
setLastAccountTreeActivity(newList); | |
} | |
public void handleBeforeUpdate(List<Account> oldList, List<Account> newList) { | |
setLastAccountTreeActivity(newList); | |
} | |
// Populates Last_Account_Tree_Activity__c with the time now when the account is created or updated | |
private void setLastAccountTreeActivity(List<Account> newList) { | |
Datetime timeNow = Datetime.now(); | |
for(Account acc : newList) { | |
acc.Last_Account_Tree_Activity__c = timeNow; | |
} | |
} | |
} |
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 with sharing class AccountHandlerUpdateParents implements AfterInsert, AfterUpdate { | |
static Boolean hasRun = false; // variable illustrating a simple way to stop recursion | |
public void handleAfterInsert(List<Account> newList) { | |
updateParentAccounts(newList); | |
} | |
public void handleAfterUpdate(List<Account> oldList, List<Account> newList) { | |
updateParentAccounts(newList); | |
} | |
// Populates Last_Account_Tree_Activity__c on parent accounts to the time now | |
private void updateParentAccounts(List<Account> newList) { | |
if(!hasRun) { | |
Set<Id> accountIdToProcess = new Set<Id>(); | |
for(Account newAcc : newList) { | |
if(newAcc.ParentId != null) { | |
accountIdToProcess.add(newAcc.ParentId); | |
} | |
} | |
List<Account> accountsToUpdate = new List<Account>(); | |
for(Id parentId : accountIdToProcess) { | |
Datetime timeNow = Datetime.now(); | |
Account parentAcc = new Account(Id = parentId, Last_Account_Tree_Activity__c = timeNow); | |
accountsToUpdate.add(parentAcc); | |
} | |
hasRun = true; | |
update accountsToUpdate; | |
} | |
} | |
} |
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 MetaDataTriggerManager(Account.SObjectType)).handle(); | |
} |
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
@JsonAccess(deserializable='always') | |
public with sharing class ContactHandlerUpdateAccount implements AfterInsert { | |
public static String mySpecialParameter; | |
public void handleAfterInsert(List<Contact> newList) { | |
System.debug('mySpecialParameter: '+mySpecialParameter); | |
updateAccount(newList); | |
} | |
public void handleAfterUpdate(List<Contact> oldList, List<Contact> newList) { | |
updateAccount(newList); | |
} | |
static Boolean hasRun = false; // variable illustrating a simple way to stop recursion | |
public void updateAccount(List<Contact> newList) { | |
if(!hasRun) { | |
Set<Id> accountIds = new Set<Id>(); | |
for(Contact con : newList) { | |
accountIds.add(con.AccountId); | |
} | |
List<Account> accountsList = new List<Account>(); | |
Datetime timeNow = Datetime.now(); | |
for(Id accountId : accountIds) { | |
Account acc = new Account(Id = accountId, Last_Account_Tree_Activity__c = timeNow); | |
accountsList.add(acc); | |
} | |
hasRun = true; | |
update accountsList; | |
} | |
} | |
} |
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 ContactTrigger on Contact (before insert, before update, before delete, after insert, after update, after delete, after undelete) { | |
(new MetaDataTriggerManager(Contact.SObjectType)).handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment