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
Michael Hedrick 2Michael Hedrick 2 

Code Coverage issue in Trigger calling Class

Hello,
I have a trigger that is not not reaching the 75% threshold.
I have 2 lines of code where I am calling another Class.  Any help would be greatly appreciated.
trigger warrantyValueCalculation on Warranties_and_Surveys__c (before insert, before update) {
    for (Warranties_and_Surveys__c warRec: trigger.new)
    {
         if((trigger.isInsert) && (warRec.Warranty_Survey_Account__c=='123' || warRec.Warranty_Survey_Account__c=='456' || warRec.Warranty_Survey_Account__c=='789' || warRec.Warranty_Survey_Account__c=='136' || warRec.Warranty_Survey_Account__c=='764' || warRec.Warranty_Survey_Account__c=='111')&&(warRec.Project_Country__c=='US')){
         warrantyAmountCalculation.beforeInsert(trigger.new); ---no coverage
        }
        //if((trigger.isUpdate) && ((trigger.oldMap.get(warRec.Id).Registration_Status__c == 'Registered') && (warRec.Registration_Status__c == 'Registered'))){
         if((trigger.isUpdate) && (warRec.Warranty_Survey_Account__c=='001f100001M2FVP' || warRec.Warranty_Survey_Account__c=='876' || warRec.Warranty_Survey_Account__c=='590' || warRec.Warranty_Survey_Account__c=='327' || warRec.Warranty_Survey_Account__c=='970' || warRec.Warranty_Survey_Account__c=='327')&&(warRec.Project_Country__c=='US')){
         warrantyAmountCalculation.beforeInsert(trigger.new); ---no coverage
        }
    }

}

 
Best Answer chosen by Michael Hedrick 2
Maharajan CMaharajan C
Change your trigger like below:

trigger warrantyValueCalculation on Warranties_and_Surveys__c (before insert, before update) {
    
    if (Trigger.isInsert)
    {
        for (Warranties_and_Surveys__c warRec: trigger.new)
        {
            if(warRec.Warranty_Survey_Account__c=='123' || warRec.Warranty_Survey_Account__c=='456' || warRec.Warranty_Survey_Account__c=='789' || warRec.Warranty_Survey_Account__c=='136' || warRec.Warranty_Survey_Account__c=='764' || warRec.Warranty_Survey_Account__c=='111')&&(warRec.Project_Country__c=='US')){
             warrantyAmountCalculation.beforeInsert(trigger.new);
            }
        }
    }
    
    else if(Trigger.isUpdate)
    {
        for (Warranties_and_Surveys__c warRec: trigger.new)
        {
            if((warRec.Warranty_Survey_Account__c=='001f100001M2FVP' || warRec.Warranty_Survey_Account__c=='876' || warRec.Warranty_Survey_Account__c=='590' || warRec.Warranty_Survey_Account__c=='327' || warRec.Warranty_Survey_Account__c=='970' || warRec.Warranty_Survey_Account__c=='327')&&(warRec.Project_Country__c=='US')){
             warrantyAmountCalculation.beforeInsert(trigger.new); 
            }
        }
    }
}

Thanks,
Maharajan.C

All Answers

Vivek NayakVivek Nayak
Can you share your test class and warrantyAmountCalculation class? Also what message you are getting in logs while runnig test?

Do following if you haven't done:
Write test class for trigger and create test data using @TestSetup.
insert  and update atleast 20 records. 
Make sure you are passing list parameter in beforeInsert method.
Make sure you are not using any custom setting in beforeInsert method..
Use asertions properly.

Thanks
Maharajan CMaharajan C
Hi Michael,

Please use the test class like below:

private class TestClassChatterPosts {

  static testMethod void testWarrantiesSurveyonInsert() 
    {
        Warranties_and_Surveys__c ws = new Warranties_and_Surveys__c();
        ws.Warranty_Survey_Account__c = '123';
        ws.Project_Country__c = 'US';
        // Please Add the remaining required fields like above to insert the Warranties_and_Surveys__c records.
        
        Insert ws;

    }
    
    static testMethod void testWarrantiesSurveyonUpdate() 
    {
        Warranties_and_Surveys__c ws = new Warranties_and_Surveys__c();
        ws.Warranty_Survey_Account__c = '123';
        ws.Project_Country__c = 'US';
        // Please Add the remaining required fields like above to insert the Warranties_and_Surveys__c records.
        
        Insert ws;
        
        ws.Warranty_Survey_Account__c = '876';
        update ws;
    }
}


Thanks,
Maharajan.C
Michael Hedrick 2Michael Hedrick 2
Thank you both for the reply.  Maharajan, your example is similiar to what you have posted but it still skips over the 
'warrantyAmountCalculation.beforeInsert(trigger.new);' line of code in the trigger.

Cheers,
M
Maharajan CMaharajan C
Michael please check the test class passed sucessfully or not. While run the test class are u getting any error.  
Michael Hedrick 2Michael Hedrick 2
No errors but only 66% coverage.  The trigger is calling another Class butthat class has 100% coverage.....Not sure what the issue is.
Thank you for your help.
M
Maharajan CMaharajan C
Change your trigger like below:

trigger warrantyValueCalculation on Warranties_and_Surveys__c (before insert, before update) {
    
    if (Trigger.isInsert)
    {
        for (Warranties_and_Surveys__c warRec: trigger.new)
        {
            if(warRec.Warranty_Survey_Account__c=='123' || warRec.Warranty_Survey_Account__c=='456' || warRec.Warranty_Survey_Account__c=='789' || warRec.Warranty_Survey_Account__c=='136' || warRec.Warranty_Survey_Account__c=='764' || warRec.Warranty_Survey_Account__c=='111')&&(warRec.Project_Country__c=='US')){
             warrantyAmountCalculation.beforeInsert(trigger.new);
            }
        }
    }
    
    else if(Trigger.isUpdate)
    {
        for (Warranties_and_Surveys__c warRec: trigger.new)
        {
            if((warRec.Warranty_Survey_Account__c=='001f100001M2FVP' || warRec.Warranty_Survey_Account__c=='876' || warRec.Warranty_Survey_Account__c=='590' || warRec.Warranty_Survey_Account__c=='327' || warRec.Warranty_Survey_Account__c=='970' || warRec.Warranty_Survey_Account__c=='327')&&(warRec.Project_Country__c=='US')){
             warrantyAmountCalculation.beforeInsert(trigger.new); 
            }
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Thanks Maharajan.