+ Start a Discussion
John NeilanJohn Neilan 

Help To Increase Test Coverage

Hello,

I have created a trigger that fires when a Task is Inserted, Updated, or Deleted. When it fires, it updates custom checkboxes on open Opportunities based upon the Subject of the Task and checks to see that the ActivityDate is within the effective and renewal dates of the Opp. All works fine, however, I am not able to get my test to cover the part of the code that works on checking one of the boxes after an update. Can anyone advise on how I can get the code covered below (Specifically line 14 and lines 19-37 of the Trigger Class?

Trigger:
IF(Trigger.IsUpdate){
    ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount();
    updater4.taskCountUpdate(Trigger.new,Trigger.old);
}

Trigger Class:
public class ClassRenewalTaskCount {

Set<ID> OppIds = new Set<ID>();
Set<ID> TaskIdsRem = new Set<ID>();

    String acctPrefix = Account.SObjectType.getDescribe().getKeyPrefix();

public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) {

    for (Task t : checkTasks) {
        for (Task prev : oldTasks){
            //Adds a task that is updated to completed and current
            if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) {
                OppIds.add(t.WhatId);              
            }
        }
    }

    if (OppIds.size() > 0){

        List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks)
                                        FROM Account
                                        WHERE Id IN : OppIds];

            List<Opportunity> oppsUpdatable = new List<Opportunity>();

                for(Account acct : acctsWithTasks){
                    for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon
                                        FROM Opportunity
                                        WHERE Account.Id =: acct.Id AND IsWon=FALSE]){
                    L.RP_Customer_Engagement__c = acct.Tasks.size();
                    oppsUpdatable.add(L);
                    }
                }

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

Test Method:
static testmethod void testRenewalTasks3(){

    Account acct1 = TestCreateRecords.createAcct(0);
    insert acct1;

    Opportunity opp1 = TestCreateRecords.createOppNew(acct1.Id);
    insert opp1;
        opp1.RP_Customer_Engagement__c = 0;
        UPDATE opp1;

    Task task1 = TestCreateRecords.createTaskInitProg(acct1.Id);
    INSERT task1;

    Task task2 = new task();
    task2.WhatId = acct1.Id;
    task2.ActivityDate = Date.today().addDays(10);
    task2.Subject = 'User Training Complete';
    task2.Status = 'In Progress';
    task2.Priority = 'Normal';
    INSERT task2;


Test.startTest();
    Task tsk1 = [SELECT Id,ActivityDate,Status
                FROM Task
                WHERE Id =: task1.Id];
    tsk1.Status = 'In Progress';
    UPDATE tsk1;

    Task tsk2 = [SELECT Id,ActivityDate,Status
                FROM Task
                WHERE Id =: tsk1.Id];
    tsk2.Status = 'Completed';
    UPDATE tsk2;
Test.stopTest();
}

 
Best Answer chosen by John Neilan
John NeilanJohn Neilan
FYI, I got this to work.  For some reason, the test was only considering the creation of Task1 and the update of tsk1.  I separated Task2 and tsk2 into another test and the code was covered 100%.  Thanks for the assistance!

All Answers

HawkedHawked
The naming used in the trigger is super confusing, you are building an account list with the name OppId :/

Having said that when you are inserting  task1, task2, tsk2

You are not associating some of the conditions you are checking in the if loop 

in your task2 insert logic add
task2.RP_Customer_Engagement__c =  Date.today().addDays(10)
task2.ActivityDate = Date.today().addDays(-10);

 
John NeilanJohn Neilan
Yes, sorry for the confusion.  It was an existing trigger that I am jst updating and I didn't get to change the oppId references yet.  RP_Customer_Engagement__c is a field on the Opportunity and is a number field, not a date field.  Line 14 OppIds.add(t.WhatId); is showing as red which leads me to believe nothing is being added to OppIds causing the rest to fail, but I can't figure out why.  Do you think it could have something to do with using trigger.old?  Thanks
HawkedHawked
My bad copied the wrong field, you are comparing the RP_Prior_60__c agains the activity date
task2.RP_Prior_60__c = Date.today().addDays(10) 
​task2.ActivityDate = Date.today().addDays(-10);

 
John NeilanJohn Neilan
Thanks.  I actually did try that previously, but the problem is that RP_Prior_60__c is a formula field (TODAY()-60) so it won't let me set a value in my test.  Do you know of a way around this?
John NeilanJohn Neilan
FYI, I got this to work.  For some reason, the test was only considering the creation of Task1 and the update of tsk1.  I separated Task2 and tsk2 into another test and the code was covered 100%.  Thanks for the assistance!
This was selected as the best answer