function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

How to test a trigger handler class

Hi there,

I have built my first trigger handler class, I am quite prouf of myself as I have attempted to do this for a long time now. The only problem is that i do not know how to test it. It passes 69% just from all my old trigger tests, however you need 75% to pass. Please help me get my last 6%. 

Also if you have time I was hoping someone could have a look and tell me how I went at combining my triggers, there are 3 combined I think in total. Thank you for your time.

This is my trigger handler:

public class TransactionTriggerHandler {

private boolean m_isExecuting = True;
private integer BatchSize = 0;
private final static boolean ISUPDATE_TRUE = true;
private final static boolean ISUPDATE_FALSE = false;

public TransactionTriggerHandler (boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
public void onBeforeInsert(list<Transaction__c> triggerNew, map<id,Transaction__c> triggerNewMap)
{
List<Commission_Period__c> comperiodlist = [Select Id, Point_Calculation_Period_Start__c, Point_Calculation_Period_End__c, Office__c from Commission_Period__c];
List<Office_Commission__c> officeCommissions = [Select Id, Commission_Period_Start__c, Commission_Period_End__c, Office__c from Office_Commission__c];
List<Account_Status__c> AccSt = new List<Account_Status__c>();
List<Account_Status__c> updatedAccSt = new List<Account_Status__c>();
List<Id> accIds = new List<Id>();
set<Id> traserviceIDset = new set<Id>();

for (Transaction__c Tra : triggerNew)
traserviceIDset.add(Tra.Destiny_Service_No__c);

map<id, service__c> servicesMap = new Map<id, Service__c>([Select id, Service_Name__c, service_type__c from Service__c where id in :traserviceIDset limit 1]);

for (Transaction__c Tra: triggerNew) {
if(Tra.Account__c != null && (Tra.Transaction_Type__c == 'First Payment'||Tra.Transaction_Type__c == 'Refund')) {


accIds.add(Tra.Account__c);
}
}
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account__c in :accIds];
for(Account_Status__c a : AccSt)
{



for (Transaction__c Tra: triggerNew ) {
//First Payment Services
//EPE Only
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Essential Property Education Only' && servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'New Client' && Tra.Transaction_Type__c == 'First Payment') {
a.Account_Status__c = 'EPE Course Only';
a.EPE_Course_Only__c = date.today();
}
//Adv New Client
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Advantage Program' && servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'New Client'&& Tra.Transaction_Type__c == 'First Payment') {
a.Account_Status__c = 'EPE Course: Advantage';
a.EPE_Course_Advantage__c = date.today();
}
//Adv Upgrade from EPE
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Advantage Program' && Tra.Transaction_Type__c == 'First Payment' && (servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Adv Upgrade EPE 1x New'||servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Adv Upgrade EPE 2x New')) {
a.Account_Status__c = 'Support: Advantage';

}
//IPC New Client
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Intensive Property Coaching' && Tra.Transaction_Type__c == 'First Payment' && servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'New Client') {
a.Account_Status__c = 'EPE Course: IPC';
a.EPE_Course_IPC__c = date.today();
}
//IPC Upgrade from EPE
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Intensive Property Coaching' && Tra.Transaction_Type__c == 'First Payment' && (servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'IPC Upgrade EPE 1x New'||servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'IPC Upgrade EPE 2x New')) {
a.Account_Status__c = 'Support: IPC';

}
//Momentum Support Standard
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Momentum Support' && Tra.Transaction_Type__c == 'First Payment' && servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Standard') {
a.Account_Status__c = 'Support: Momentum';
a.Support_Momentum__c = date.today();
}
//Momentum Support Plus
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Momentum Support' && Tra.Transaction_Type__c == 'First Payment' && (servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Plus'||servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Plus-Free')) {
a.Account_Status__c = 'Support: Momentum Plus';
a.Support_Momentum_Plus__c = date.today();
}



//Refund Services
//EPE Only Refund
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Essential Property Education Only' && Tra.Transaction_Type__c == 'Refund') {
a.Account_Status__c = 'Refund EPE Only';
a.Refund_EPE_Only__c = date.today();
}
//Adv Refund
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Advantage Program' && Tra.Transaction_Type__c == 'Refund') {
a.Account_Status__c = 'Refund Advantage';
a.Refund_Advantage__c = date.today();


}
//IPC Refund
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Intensive Property Coaching' && Tra.Transaction_Type__c == 'Refund') {
a.Account_Status__c = 'Refund IPC';
a.Refund_IPC__c = date.today();
}

//Refund Momentum Support Standard
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Momentum Support' && Tra.Transaction_Type__c == 'Refund' && servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Standard') {
a.Account_Status__c = 'Refund Momentum';
a.Refund_Momentum__c = date.today();
}
//Refund Momentum Support Plus
if(servicesMap.get(Tra.Destiny_Service_No__c).Service_Name__c == 'Momentum Support' && Tra.Transaction_Type__c == 'Refund' && (servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Plus'||servicesMap.get(Tra.Destiny_Service_No__c).Service_Type__c == 'Plus-Free')) {
a.Account_Status__c = 'Refund Momentum Plus';
a.Refund_Momentum_Plus__c= date.today();
}

}
updatedAccSt.add(a);

}
if(updatedAccSt.size()>0)
{
update updatedAccSt;
}

for(Transaction__c tra:triggernew)
{
    for(Commission_Period__c  b:comperiodlist){


if(tra.Date_of_payment__c.addMonths(-2).tostartofmonth() == b.Point_Calculation_Period_Start__c  && tra.Date_of_payment__c.addMonths(1).tostartofmonth().addDays(-1) == b.Point_Calculation_Period_End__c && tra.Office__c == b.Office__c)
            tra.Commission_Period__c = b.Id;
            
            if(tra.Date_of_payment__c.addMonths(-1).tostartofmonth() == b.Point_Calculation_Period_Start__c  && tra.Date_of_payment__c.addMonths(2).tostartofmonth().addDays(-1) == b.Point_Calculation_Period_End__c && tra.Office__c == b.Office__c)
            tra.Commission_Period_2__c = b.Id;
            
             if(tra.Date_of_payment__c.tostartofmonth() == b.Point_Calculation_Period_Start__c  && tra.Date_of_payment__c.addMonths(3).tostartofmonth().addDays(-1) == b.Point_Calculation_Period_End__c && tra.Office__c == b.Office__c)
            tra.Commission_Period_3__c = b.Id;
   




     }
     
      for(Office_Commission__c OffCom:officeCommissions){
      if(tra.Date_of_payment__c >= OffCom.Commission_Period_Start__c && tra.Date_of_payment__c <= OffCom.Commission_Period_End__c && tra.Office__c == OffCom.Office__c)
            tra.Office_Commission__c = OffCom.Id;
            
            }

}


}

public void OnAfterInsert(list<Transaction__c> triggerNew, map<id,Transaction__c> triggerNewMap)
{
/*
//CallAfterInsertMethod1formHere();

//Get the correct email template here
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'Transaction_Receipt'];

Set<Id> accountIds = new Set<Id>();
for (Transaction__c tra : triggerNew) {
accountIds.add(tra.Account__c);
}

List<Account> accounts = [SELECT Id, (SELECT Id, Email FROM Contacts) FROM Account WHERE Id IN :accountIds];
Map<Id, List<Contact>> accountMap = new Map<Id, List<Contact>>();
for (Account acct : accounts) {
List<Contact> contacts = new List<Contact>();
for (Contact c : acct.Contacts) {
contacts.add(c);
}
accountMap.put(acct.Id, contacts);
}

//It's best to make a list of emails to send, so that you can send them all at once at the end, in order to avoid hitting governor limits
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();

for (Transaction__c tra : triggerNew) {
for (Contact c : accountMap.get(tra.Account__c)) {
if (c.Email != null) {
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTemplateId(template.Id);
m.setTargetObjectId(c.Id);
m.setWhatId(tra.Id);
messages.add(m);
}
}
}
Messaging.sendEmail(messages);
*/

}

public void OnBeforeUpdate(list<Transaction__c> triggerNew, map<id,Transaction__c> triggerNewMap, map<id,Transaction__c> triggerOldMap)
{
//CallBeforeUpdateMethod1formHere();
//CallBeforeUpdateMethod2formHere();
// Query all the values from the Commision Period






List<Commission_Period__c> comperiodlist = [Select Id, Point_Calculation_Period_Start__c, Point_Calculation_Period_End__c, Office__c from Commission_Period__c];
List<Office_Commission__c> officeCommissions = [Select Id, Commission_Period_Start__c, Commission_Period_End__c, Office__c from Office_Commission__c];


for(Transaction__c tra:triggerNew)
{
for(Commission_Period__c  b:comperiodlist){


if(tra.Date_of_payment__c.addMonths(-2).tostartofmonth() == b.Point_Calculation_Period_Start__c  && tra.Date_of_payment__c.addMonths(1).tostartofmonth().addDays(-1) == b.Point_Calculation_Period_End__c && tra.Office__c == b.Office__c)
tra.Commission_Period__c = b.Id;

if(tra.Date_of_payment__c.addMonths(-1).tostartofmonth() == b.Point_Calculation_Period_Start__c  && tra.Date_of_payment__c.addMonths(2).tostartofmonth().addDays(-1) == b.Point_Calculation_Period_End__c && tra.Office__c == b.Office__c)
tra.Commission_Period_2__c = b.Id;

if(tra.Date_of_payment__c.tostartofmonth() == b.Point_Calculation_Period_Start__c  && tra.Date_of_payment__c.addMonths(3).tostartofmonth().addDays(-1) == b.Point_Calculation_Period_End__c && tra.Office__c == b.Office__c)
tra.Commission_Period_3__c = b.Id;





}

for(Office_Commission__c OffCom:officeCommissions){
if(tra.Date_of_payment__c >= OffCom.Commission_Period_Start__c && tra.Date_of_payment__c <= OffCom.Commission_Period_End__c && tra.Office__c == OffCom.Office__c)
tra.Office_Commission__c = OffCom.Id;

}

}


}

public void OnAfterUpdate(list<Transaction__c> triggerNew, map<id,Transaction__c> triggerNewMap, map<id,Transaction__c> triggerOldMap)
{
//CallAfterUpdateMethod1formHere();
//CallAfterUpdateMethod2formHere();
}

public void OnBeforeDelete(list<Transaction__c> triggerOld, map<id,Transaction__c> triggerOldMap)
{
//CallBeforeDeleteMethod1formHere();
//CallBeforeDeleteMethod2formHere();
}

public void OnAfterDelete(list<Transaction__c> triggerOld, map<id,Transaction__c> triggerOldMap)
{
//CallAfterDeleteMethod1formHere();
//CallAfterDeleteMethod2formHere();
}
}


Best Answer chosen by Developer.mikie.Apex.Student
SalesRedSalesRed
Hi,   Was your test class previously returning a higher code coverage?  if it is the exact same functionality but just moved to a handler class it may be that the coverage in general (independent of the move to the handler class) needs to be improved.

All Answers

SalesRedSalesRed
Hi,   Was your test class previously returning a higher code coverage?  if it is the exact same functionality but just moved to a handler class it may be that the coverage in general (independent of the move to the handler class) needs to be improved.
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I see and thank you. Would you possibly be able to tell me something about my code. Is it alright to have all the triggers in before insert set out like that or should I separate them completely. If so, how would I go about separating them or perhaps ordering them? Is it set out right in general or do I move all the lists and and SOQL to the top or something? Basically, I just copied and pasted the code into the sections they belonged in and changed the trigger.new to triggernew.