Skip to content

Instantly share code, notes, and snippets.

@LawrenceLoz
Last active October 28, 2021 05:40
Show Gist options
  • Save LawrenceLoz/ac2fa418fbc8bc081b47bb6b7064c5ca to your computer and use it in GitHub Desktop.
Save LawrenceLoz/ac2fa418fbc8bc081b47bb6b7064c5ca to your computer and use it in GitHub Desktop.
Trigger and handler class examples - Apex Trigger Actions
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;
}
}
}
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;
}
}
public with sharing class AccountHandlerUpdateRelatedContacts implements TriggerAction.AfterUpdate {
// Updates contacts related to the account to set Last_Account_Update__c to the current date and time
public void afterUpdate(List<Account> newList, List<Account> oldList) {
Map<Id,Account> newMap = new Map<Id,Account>(newList);
List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId IN :newMap.keySet()];
Datetime timeNow = Datetime.now();
for(Contact con : contacts) {
con.Last_Account_Update__c = timeNow;
}
update contacts;
}
}
trigger AccountTrigger on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
new MetadataTriggerHandler().run();
}
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;
}
}
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