+ Start a Discussion

Test code passing but only at 61% need 100%

Hey there,

I have a trigger which is designed to autofill 4 lookup fields. I have attempted to write a test code, it passes except that it only fills 61% code coverage. I was hoping to get some tips on how I may get this over 75% prefereably 100%.

This is my test code:

private class TestOfficeCommission_GrabReference {
    static testMethod void myGrabTest() {
        //name a Office commission record
        Date monFirstStart = System.today().addMonths(-2).toStartOfMonth();
Date monFirstEnd = monFirstStart.addDays(Date.daysInMonth(monFirstStart.year(), monFirstStart.month()) - 1);
Date monSecondStart = System.today().addMonths(-1).toStartOfMonth();
Date monSecondEnd = monFirstStart.addDays(Date.daysInMonth(monFirstStart.year(), monFirstStart.month()) - 1).addMonths(1);
Date monThirdStart = System.today().toStartOfMonth();
Date monThirdEnd = monFirstStart.addDays(Date.daysInMonth(monFirstStart.year(), monFirstStart.month()) - 1).addMonths(2);
Date monFourthStart = System.today().toStartOfMonth().addMonths(2);
Date monFourthEnd = monFirstStart.addDays(Date.daysInMonth(monFirstStart.year(), monFirstStart.month()) - 1).addMonths(3);
Date OffComStart = System.today().toStartOfMonth();
Date OffComEnd = OffComStart.addDays(Date.daysInMonth(OffComStart.year(), OffComStart.month()) - 1);

Office__c Off = new Office__c(Name = 'Test');
insert Off;

Commission_period__c NOVJAN = new Commission_period__c(Name = 'NOVJAN', Point_Calculation_Period_Start__c = monFirstStart , Point_Calculation_Period_End__c = monFirstEnd , Office__c = Off.id);
insert NOVJAN;
Commission_period__c DECFEB = new Commission_period__c(Name = 'DECFEB', Point_Calculation_Period_Start__c = monSecondStart , Point_Calculation_Period_End__c = monSecondEnd, Office__c = Off.id );
insert DECFEB;
Commission_period__c JANMAR = new Commission_period__c(Name = 'JANMAR', Point_Calculation_Period_Start__c = monThirdStart , Point_Calculation_Period_End__c = monThirdEnd, Office__c = Off.id );
insert JANMAR;
Commission_period__c FEBAPR = new Commission_period__c(Name = 'FEBAPR', Office__c = Off.id, Point_Calculation_Period_Start__c = monFourthStart, Point_Calculation_Period_End__c = monFourthEnd);
insert FEBAPR;

       Office_Commission__c OffCom = new Office_Commission__c ( Commission_Period_start__c = OffComStart, Commission_Period_End__c = OffComEnd,
       Office__c = Off.id);
        insert OffCom;
        //build a test dp record with the picklist field set to your previous record

        //query for the record, now that it's been inserted and the trigger has been fired

        OffCom = [ SELECT Bonus_Points_In__C, Bonus_Points_out__c, Bonus_Points_out_2__c, Bonus_Points_out_3__c FROM Office_Commission__c WHERE Id = :OffCom.Id ];    

        //asert that the trigger's field change was made


This is my trigger:

trigger OfficeCommission_GrabReference on Office_Commission__c (before insert) {

    // 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];

for(Office_Commission__c Off:trigger.new)
    for(Commission_Period__c  b:comperiodlist){

// This is for your first If condition , Trying to print all the four values to compare and decide whether it is getting inside the if condition or not. if it is not getting inside the if condition obviously it wont populate the value.

if(Off.Commission_Period_Start__c.addMonths(-2).month() == b.Point_Calculation_Period_Start__c.month()  && Off.Commission_Period_End__c.month() == b.Point_Calculation_Period_End__c.month() && Off.Office__c == b.Office__c)
            Off.Bonus_Points_Out__c = b.Id;

if(Off.Commission_Period_Start__c.addMonths(-1).month() == b.Point_Calculation_Period_Start__c.month()  && Off.Commission_Period_End__c.addMonths(1).month() == b.Point_Calculation_Period_End__c.month() && Off.Office__c == b.Office__c)
            Off.Bonus_Points_Out_2__c = b.Id;

if(Off.Commission_Period_Start__c.month() == b.Point_Calculation_Period_Start__c.month()  && Off.Commission_Period_End__c.addMonths(2).month() == b.Point_Calculation_Period_End__c.month()&& Off.Office__c == b.Office__c)
            Off.Bonus_Points_Out_3__c = b.Id;
if(Off.Commission_Period_Start__c.addMonths(-3).month() == b.Point_Calculation_Period_Start__c.month()  && Off.Commission_Period_End__c.addMonths(-1).month() == b.Point_Calculation_Period_End__c.month() && Off.Office__c == b.Office__c)
            Off.Bonus_Points_In__c = b.Id;



Best Answer chosen by Developer.mikie.Apex.Student
You dont need coverage for Sandbox, only for production you need atlest 1% for trigger and 75% for class.

All Answers

Trigger just need 1% for deployment. with 61% you will be able to deploy happily.

Mark this as answer if it helps you..
Thank you PeaceMaker,

Even from sandbox?

Can you think of anyways I still might be able to increase the code coverage?
You dont need coverage for Sandbox, only for production you need atlest 1% for trigger and 75% for class.
This was selected as the best answer
That is brilliant! Will definitely be useful in my upcoming cert test. Thank you PeaceMaker