1

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.parse('1/5/15');
    task2.Subject = 'Offerpop 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();
}
2
  • 4
    Get those queries out of for loops!!! – Adrian Larson Jan 29 '15 at 16:43
  • n.b. testmethods without asserts aren't best practice. If the code changes over time, you'll never know if it is broken until a user reports some error – cropredy Jan 30 '15 at 22:55
1

Your filter needs to return results:

if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) )

In your test, you never set Task.RP_Prior_60__c. Hence no task meets the entire set of criteria.

It may be easier to test this if you separate your two concerns, getting the relevant Id set and then acting on those Ids.

static Set<Id> getAccountIds(List<Task> newTasks, List<Task> oldTasks)
{
    // perform filtering
    // do you really need the old records?
}

static void updateTaskCount(Set<Id> accountIds)
{
    // perform action
}
3
  • Thanks Adrian, I tried that but RP_Prior_60__c is a calculated field (TODAY()-60) so it will not let me set a value for it. Is there a workaround for that? – John N. Jan 29 '15 at 17:52
  • Well, you definitely should use a more dynamic ActivityDate like Date.today().addMonths(1) or some such. Try adding it to your query perhaps? You will have an easier time if you move the filtering to a top level method that you test directly. – Adrian Larson Jan 29 '15 at 17:59
  • Thanks. I did change the dates, but it didn't affect my test coverage. I'm not sure what you mean by a "top level" method. Full disclosure, I am an Admin who know some Apex. This trigger and class is one that someone else created and left the company. I'm just trying to get it moved to production. Thanks for all your help! – John N. Jan 29 '15 at 19:38
0

Just as a follow up, I got my test to 100%. 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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.