My trigger handler and simple test class (Credit goes to many people for this)
Trigger
trigger AccountTrigger on Account (after delete, after insert, after undelete, after update, before delete, before insert, before update) { //Helper Class AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size); //INSERT if(Trigger.isInsert && Trigger.isBefore){ handler.OnBeforeInsert(Trigger.new, Trigger.newMap); }else if(Trigger.isInsert && Trigger.isAfter){ handler.OnAfterInsert(Trigger.new, Trigger.newMap); //UPDATE }else if(Trigger.isUpdate && Trigger.isBefore){ handler.OnBeforeUpdate(Trigger.old, Trigger.oldMap, Trigger.new, Trigger.newMap); }else if(Trigger.isUpdate && Trigger.isAfter){ handler.OnAfterUpdate(Trigger.old, Trigger.oldMap, Trigger.new, Trigger.newMap); //DELETE }else if(Trigger.isDelete && Trigger.isBefore){ handler.OnBeforeDelete(Trigger.old, Trigger.oldMap); }else if(Trigger.isDelete && Trigger.isAfter){ handler.OnAfterDelete(Trigger.old, Trigger.oldMap); //UNDELETE }else if(Trigger.isUnDelete){ handler.OnUndelete(Trigger.new); } }
Trigger Helper Class
public with sharing class AccountTriggerHandler { private boolean m_isExecuting = false; private integer BatchSize = 0; public AccountTriggerHandler(boolean isExecuting, integer size){ m_isExecuting = isExecuting; BatchSize = size; } //trigger Consuming Methods public void OnBeforeInsert(list<Account> newRecords,Map<ID, Account> newMap){ // EXECUTE LOGIC } public void OnAfterInsert(list<Account> newRecords,Map<ID, Account> newMap){ // EXECUTE LOGIC } public void OnBeforeUpdate(list<Account> oldRecords, Map<ID, Account> oldMap, list<Account> updatedRecords, Map<ID, Account> newMap){ // EXECUTE LOGIC } public void OnAfterUpdate(list<Account> oldRecords, Map<ID, Account> oldMap, list<Account> updatedRecords, Map<ID, Account> newMap){ // EXECUTE LOGIC } public void OnBeforeDelete(list<Account> RecordsToDelete, Map<ID, Account> newMap){ // EXECUTE LOGIC } public void OnAfterDelete(list<Account> deletedRecords, Map<ID, Account> newMap){ // EXECUTE LOGIC } public void OnUndelete(list<Account> restoredRecords){ // EXECUTE LOGIC } }
Test Class
@istest public with sharing class TestAccountTriggerHandler{ static testmethod void runTest(){ list<Account> Accounts = InsertTest(); Accounts = UpdateTest(Accounts); Accounts = DeleteTest(Accounts); Accounts = UndeleteTest(Accounts); } static list<Account> InsertTest(){ list<Account> Accounts = new list<Account>(); for(Integer x=0; x<200;x++){ Accounts.add(new Account(Name = 'Test'+x)); } insert Accounts; system.assert(Accounts.size()==200,'Issue with inserting accounts'); return accounts; } static list<Account> UpdateTest(list<Account> accounts){ //Update Accounts for(account a:accounts){ a.website= 'http://' + a.name + '.com' ; } update Accounts; system.assert(Accounts.size()==200,'Issue with updating accounts'); return accounts; } static list<Account> DeleteTest(list<Account> accounts){ //Delete Accounts delete accounts; system.assert(Accounts.size()==200,'Issue with deleting accounts'); return [select id from account where isdeleted=true ALL ROWS]; } static list<Account> UndeleteTest(list<Account> accounts){ //Undelete Accounts undelete accounts; system.assert(Accounts.size()==200,'Issue with undeleting accounts'); return accounts; } }
Comments
Post a Comment