+ Start a Discussion
Jon RodigerJon Rodiger 

Help needed with updating lead status from task trigger and testing

I am trying to update the status of a lead from a task trigger if a certain task is completed. When testing my code the execution always seems to stop when I try to update the lead. I think this may be because in my test class the tasks are not assigned to a lead, but when I tried creating a sample lead to use I think I made a mistake because the test then gets 0% code coverage.
trigger TaskScheduler on Task (after insert, after update) {
    // Iterate through all tasks
    for(Task t : Trigger.new){
        // Schedule follow up task for completed tasks depending on task subject
        if(t.Status == 'Completed'){    
            if(t.Subject == 'Send Initial Email'){
                // update lead status
                Lead myLead = new Lead(Id = t.whoId);
                myLead.Id = t.whoId;
                myLead.Status = 'Working';
                update myLead;
                // schedule new task
                Task newTask = new Task(Subject = 'Make Initial Phone Call', IsReminderSet = True, ReminderDateTime = System.now(), Activitydate = System.now().date().addDays(1), WhoId = t.WhoId, ownerId = t.ownerId);
                insert newTask;
            } else if(t.Subject == 'Make Initial Phone Call'){
                Task newTask = new Task(Subject = 'Send Email #2', IsReminderSet = True, ReminderDateTime = System.now(), Activitydate = System.now().date(), WhoId = t.WhoId, ownerId = t.ownerId);
                insert newTask;
            } else if(t.Subject == 'Send Email #2'){
                Task newTask = new Task(Subject = 'Make Phone Call #2', IsReminderSet = True, ReminderDateTime = System.now(), Activitydate = System.now().date().addDays(1), WhoId = t.WhoId, ownerId = t.ownerId);
                insert newTask;
            } else if(t.Subject == 'Make Phone Call #2'){
                Task newTask = new Task(Subject = 'Send Email #3', IsReminderSet = True, ReminderDateTime = System.now(), Activitydate = System.now().date(), WhoId = t.WhoId, ownerId = t.ownerId);
                insert newTask;
            } else if(t.Subject == 'Send Email #3'){
                Task newTask = new Task(Subject = 'Make Phone Call #3', IsReminderSet = True, ReminderDateTime = System.now(), Activitydate = System.now().date().addDays(1), WhoId = t.WhoId, ownerId = t.ownerId);
                insert newTask;
            // When final task is completed, change lead status to 'unresponsive'
            } else if(t.Subject == 'Make Phone Call #3'){
                // update lead status
                Lead myLead2 = new Lead(Id = t.whoId);             
                myLead2.Id = t.whoId;     
                myLead2.Status = 'Unresponsive';
                update myLead2;
            }
        }    
    }
}

My test class so far:
@isTest
private class TestTaskScheduler {
    public static testMethod void validateScheduler(){
        // Start Test
        System.Test.startTest();
        Lead myLead = new Lead();
        String userId = UserInfo.getUserId();
        List<Task> taskList = new List<Task>();
        for(integer i = 0; i < 6; i++){
            Task t = new Task();
            t.OwnerId = userId;
            t.Subject = 'Send Initial Email';
            t.priority= 'Normal';
            t.status = 'Completed';
            t.description = 'Testing';
            t.ActivityDate = system.now().Date();
            t.whoId = myLead.Id;
            taskList.add(t);
        }
        taskList[1].Subject = 'Make Initial Phone Call';
        taskList[2].Subject = 'Send Email #2';
        taskList[3].Subject = 'Make Phone Call #2';
        taskList[4].Subject = 'Send Email #3';
        taskList[5].Subject = 'Make Phone Call #3';
        insert taskList;
        System.Test.stopTest(); 
    }
}

Thanks!
Best Answer chosen by Jon Rodiger
ForceMantis (Amit Jain)ForceMantis (Amit Jain)
In your test class line 6 you are creating lead but not inserting it. So Line 17 of test class where you try to get mylead.Id and assign it to t.WhoId will assign null value to t.whoId because lead is not yet inserted in database and it's id is null. To fix this you need to replace line 6 with following code.
 
Lead myLead = new Lead(LastName = 'Test', Company = 'Test Company');
insert mylead;
Hope this helps.