Last active
October 28, 2021 05:40
-
-
Save LawrenceLoz/ac2fa418fbc8bc081b47bb6b7064c5ca to your computer and use it in GitHub Desktop.
Trigger and handler class examples - Apex Trigger Actions
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 TriggerAction.BeforeInsert, TriggerAction.BeforeUpdate { | |
public void beforeInsert(List<Account> newList) { | |
setLastAccountTreeActivity(newList); | |
} | |
public void beforeUpdate(List<Account> newList, List<Account> oldList) { | |
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 TriggerAction.AfterInsert, TriggerAction.AfterUpdate { | |
public void afterInsert(List<Account> newList) { | |
updateParentAccounts(newList); | |
} | |
public void afterUpdate(List<Account> newList, List<Account> oldList) { | |
updateParentAccounts(newList); | |
} | |
// Populates Last_Account_Tree_Activity__c on parent accounts to the time now | |
private void updateParentAccounts(List<Account> newList) { | |
Set<Id> accountIdToProcess = new Set<Id>(); | |
for(Account newAcc : newList) { | |
if(newAcc.ParentId != null && TriggerBase.idToNumberOfTimesSeenAfterUpdate.get(newAcc.ParentId) == 1) { | |
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); | |
} | |
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 MetadataTriggerHandler().run(); | |
} |
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 ContactHandlerUpdateAccount implements TriggerAction.AfterInsert, TriggerAction.AfterUpdate { | |
public void afterInsert(List<Contact> newList) { | |
updateAccount(newList); | |
} | |
public void afterUpdate(List<Contact> newList, List<Contact> oldList) { | |
updateAccount(newList); | |
} | |
public void updateAccount(List<Contact> newList) { | |
Set<Id> accountIdsToUpdate = new Set<Id>(); | |
for(Contact con : newList) { | |
System.debug('Processed records after insert: '+TriggerBase.idToNumberOfTimesSeenAfterUpdate.values()); | |
if(!TriggerBase.idToNumberOfTimesSeenAfterUpdate.containsKey(con.AccountId) | |
|| TriggerBase.idToNumberOfTimesSeenAfterUpdate.get(con.AccountId) == 1) { | |
accountIdsToUpdate.add(con.AccountId); | |
} | |
} | |
List<Account> accountsList = new List<Account>(); | |
Datetime timeNow = Datetime.now(); | |
for(Id accountId : accountIdsToUpdate) { | |
Account acc = new Account(Id = accountId, Last_Account_Tree_Activity__c = timeNow); | |
accountsList.add(acc); | |
} | |
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 MetadataTriggerHandler().run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment