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
Craig GroveCraig Grove 

code coverage issue

Hi all, I'm having a code coverage issue that I'm hoping you can assist with. I have issues in the same place in a few similarly formulated triggers. Please see the below, error points are bolded:


trigger InsideSalesRollover on Inside_Sales__c (after Update) {
    List<Inside_Sales__c> rollover = new List<Inside_Sales__c>();
    
    for(Inside_Sales__c updatedsales : Trigger.New){
        if(updatedsales.Roll_Over__c == True)
        if(updatedsales.Rotation_Period__c == 'Q1'){
            rollover.add(new Inside_Sales__c(
                Rolled_Over__c = True,
                Sales_Manager__c = updatedsales.Sales_Manager__c,
                Telemarketer__c = updatedsales.Telemarketer__c,
                RecordTypeid = updatedsales.RecordTypeid,
                Account_Name__c = updatedsales.Account_Name__c,
                Status__c = 'Pending Approval',
                Rotation_Period__c = 'Q2',
                RG_Approver__c = updatedsales.RG_Approver__c,
                BD_Approver__c = updatedsales.BD_Approver__c,
                AA_Approver__c = updatedsales.AA_Approver__c));           
        } 

        if(updatedsales.Roll_Over__c == True)
        if(updatedsales.Rotation_Period__c == 'Q2'){
            rollover.add(new Inside_Sales__c(
                Rolled_Over__c = True,
                Sales_Manager__c = updatedsales.Sales_Manager__c,
                Telemarketer__c = updatedsales.Telemarketer__c,
                RecordTypeid = updatedsales.RecordTypeid,
                Account_Name__c = updatedsales.Account_Name__c,
                Status__c = 'Pending Approval',
                Rotation_Period__c = 'Q3',
                RG_Approver__c = updatedsales.RG_Approver__c,
                BD_Approver__c = updatedsales.BD_Approver__c,
                AA_Approver__c = updatedsales.AA_Approver__c)); 
        }   
        
        if(updatedsales.Roll_Over__c == True)
        if(updatedsales.Rotation_Period__c == 'Q3'){
            rollover.add(new Inside_Sales__c(
                Rolled_Over__c = True,
                Sales_Manager__c = updatedsales.Sales_Manager__c,
                Telemarketer__c = updatedsales.Telemarketer__c,
                RecordTypeid = updatedsales.RecordTypeid,
                Account_Name__c = updatedsales.Account_Name__c,
                Status__c = 'Pending Approval',
                Rotation_Period__c = 'Q4',
                RG_Approver__c = updatedsales.RG_Approver__c,
                BD_Approver__c = updatedsales.BD_Approver__c,
                AA_Approver__c = updatedsales.AA_Approver__c));
        }
        
        if(updatedsales.Roll_Over__c == True)
        if(updatedsales.Rotation_Period__c == 'Q4'){
            rollover.add(new Inside_Sales__c(
                Rolled_Over__c = True,
                Sales_Manager__c = updatedsales.Sales_Manager__c,
                Telemarketer__c = updatedsales.Telemarketer__c,
                RecordTypeid = updatedsales.RecordTypeid,
                Account_Name__c = updatedsales.Account_Name__c,
                Status__c = 'Pending Approval',
                Rotation_Period__c = 'Q1',
                RG_Approver__c = updatedsales.RG_Approver__c,
                BD_Approver__c = updatedsales.BD_Approver__c,
                AA_Approver__c = updatedsales.AA_Approver__c));
        }       
    }
    insert rollover;   
}

Here is the test class:

@IsTest
private class InsideSalesRollover {

    static testmethod void InsideSalesRollover() {
        
         Region__c reg = new Region__c(
            name = 'Test Region');
        insert reg;
            
        Account acc = new Account(
            name = 'Test Account',
            Practice_Location_Street__c = '123 My Way',
            Practice_Location_City__c = 'Farmville',
            Practice_Location_Zip_Code__c = '12345',
            Practice_Location_State__c = 'Florida',
            PDX_Region__c = reg.id);
        insert acc;  
        
        RecordType rtype = [Select name From RecordType where sObjectType='Inside_Sales__c' and isActive=true and name='Business Expansion'];
        
        Inside_Sales__c sales = new Inside_Sales__c();
            sales.RecordType = rtype;
            sales.Purpose__c = 'Schedule Meeting';
            sales.Account_Name__c = acc.id;
            sales.Status__c = 'Pending Approval';
        insert sales;
        
            sales.status__c = 'Rejected';
        update sales;
    }
}
 
Best Answer chosen by Craig Grove
James LoghryJames Loghry
Same thing.  You need to create additional test methods that provide the scenarios you aren't covering.  You do this by creating or updating the data in a fashion that will reach those else statements in your trigger.  Whatever the workflow process is that triggers your Rotation Period to update to Q2, Q3, Q4, you need to replicate the in at least one unit test.  Likely, you will need one test for each QX period.

The same thing applies to your last example.  You have two different scenarios here.  You are testing an upate where the Outcome__c is 'Meeting Scheduled', but are not testing the 'Present.Scheduled' scenario.  Once again, you need to create an additional test method where your Opportunity update meets this scenario.

Alternatively, you could try and rewrite your trigger logic to remove all the IF / ELSE blocks and potentially replace them with ternary operators for specific fields that are different between each block.  If you keep your code as simple as possible (apply the KISS principle so to speak), then you're less likely to have issues like this.

All Answers

James LoghryJames Loghry
Craig,

Take a second look at your if statements.  The reason your test does not cover everything is because you're not updating any records with a Rotation Period of Q2 or Q3 or Q4.  Either add these sales records to your unit test above, or create additional unit test methods to handle each quarter / rotation period.
Craig GroveCraig Grove
Thank you James. In the test class, marking the status as rejected triggers a standard workflow field update to update the current record to rollover, which should then trigger this apex trigger to create the new Inside Sales record. The apex trigger should, when creating the new Inside Sales record, check the existing Rotation Period and create the new Inside Sales record with the next Q*. The first instance of the rollover.add(new Inside_Sales__c( seemed to work without error, but the next 3 failed. Please let me know if I'm misunderstanding.

Standard workflowField update
Craig GroveCraig Grove
Here is a picture of another example. It looks as though the list may not be able to be referenced more than once? Maybe I have to use ELSE?

Opp Triger
James LoghryJames Loghry
Same thing.  You need to create additional test methods that provide the scenarios you aren't covering.  You do this by creating or updating the data in a fashion that will reach those else statements in your trigger.  Whatever the workflow process is that triggers your Rotation Period to update to Q2, Q3, Q4, you need to replicate the in at least one unit test.  Likely, you will need one test for each QX period.

The same thing applies to your last example.  You have two different scenarios here.  You are testing an upate where the Outcome__c is 'Meeting Scheduled', but are not testing the 'Present.Scheduled' scenario.  Once again, you need to create an additional test method where your Opportunity update meets this scenario.

Alternatively, you could try and rewrite your trigger logic to remove all the IF / ELSE blocks and potentially replace them with ternary operators for specific fields that are different between each block.  If you keep your code as simple as possible (apply the KISS principle so to speak), then you're less likely to have issues like this.
This was selected as the best answer
Craig GroveCraig Grove
Thanks again James! I appreciate your wisdom. I've fixed one, we'll see how the rest go :^)