You need to sign in to do that
Don't have an account?
Craig 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;
}
}
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;
}
}
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
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.
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.