Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LawrenceLoz/95445c8d6811e7ddb99963023415d701 to your computer and use it in GitHub Desktop.
Save LawrenceLoz/95445c8d6811e7ddb99963023415d701 to your computer and use it in GitHub Desktop.
Trigger and handler class examples - Nebula Core
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;
}
}
}
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;
}
}
}
public with sharing class AccountHandlerUpdateRelatedContacts implements AfterUpdate {
// Updates contacts related to the account to set Last_Account_Update__c to the current date and time
public void handleAfterUpdate(List<Account> oldList, List<Account> newList) {
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 MetaDataTriggerManager(Account.SObjectType)).handle();
}
@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;
}
}
}
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