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
tejateja 

Open Task Count Of Leads

I got assigned a work to implement the open task count of leads.So i implemented the trigger and wrote a test class

 

I got a test coverage of 78%. so i deployed it to the production and the admin is saying he got the following errors:
 
 
: TBN_CountCompletedCallsPerOpptyBatch.test_TBN_CountCompletedCallsPerOpptyBatch() Class 120 1 Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateLeadOpenTasksCount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateLeadOpenTasksCount: line 12, column 1: []", Failure St...
TBN_TaskContactAssignTriggerHandler.test_TBN_TaskContactAssignTriggerHandler() Class 113 1 Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateLeadOpenTasksCount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateLeadOpenTasksCount: line 12, column 1: []", Failure St...
 
 
Trigger:
 
trigger UpdateLeadOpenTasksCount on Task (after delete, after insert, after undelete,
after update) {

// Declare the variables

public set<Id> LeadIDs = new Set<Id>();
public list<Lead> LeadsToUpdate = new List<Lead>();

// Build the list of Leads to update
if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
    for(Task t: Trigger.new){
    if(string.valueOf(t.WhoId).startsWith('00Q'))
    LeadIDs.add(t.WhoId);
    }
}

if(Trigger.isDelete || Trigger.isUpdate){
    for(Task t: Trigger.old){
    if(string.valueOf(t.WhoId).startsWith('00Q'))
    LeadIDs.add(t.WhoId);
    }
}

// Update the Leads

if(LeadIDs.size()>0){
for(Lead l: [Select l.Id, l.Open_Tasks_Count__c,
(Select Id From Tasks where IsClosed = False)
From Lead l where Id in :LeadIDs])
LeadsToUpdate.add(new Lead(Id=l.Id, Open_Tasks_Count__c = l.Tasks.size()));
update LeadsToUpdate;
}
}
 
Test class:
 
@isTest
private class TestOpenTasksCountUpdate {
static testMethod void LeadTest(){
// Create a Lead
Lead newLead = new Lead(LastName='Test', Company='ABC', Status='Targeted');
insert newLead;

// Create a Task
Task newTask = new Task(Subject='Open Tasks Count', Status='Not Started', WhoId=newLead.Id);
test.startTest();
insert newTask;
test.stopTest();

// Verify that the # Open Tasks Count is correct
newLead = [select Open_Tasks_Count__c from Lead where Id=:newLead.Id ];
System.assertEquals(1,newLead.Open_Tasks_Count__c);
}

static testMethod void CompletedTest(){
// Create a Lead
Lead newLead = new Lead(LastName='Test', Company='ABC', Status='Targeted');
insert newLead;

// Create a Completed Task
Task newTask = new Task(Subject='Open Tasks Count', Status='Completed', WhoId=newLead.Id);
test.startTest();
insert newTask;
test.stopTest();

// Verify that the # Open Tasks is empty
newLead = [select Open_Tasks_Count__c from Lead where Id=:newLead.Id ];
System.assertEquals(0,newLead.Open_Tasks_Count__c);
}

static testMethod void BatchTest(){
// Create 100 Leads
List<Lead> newLeads = new List<Lead>();
for (Integer i = 0; i < 200; i++) {
newLeads.add(new Lead(LastName='Test '+i, Company='ABC', Status='Targeted'));
}
insert newLeads;

// Create a task for each one
List<Task> newTasks = new List<Task>();
for(Lead l : newLeads){
newTasks.add(new Task(Subject='Open Tasks Count', Status='Completed', WhoId=l.Id));
}

// Insert the tasks
test.startTest();
insert newTasks;
test.stopTest();

// We could verify that the Open Tasks fields were updated correctly,
// but this is just testing the governor limits so it's not necessary
}
}
 
Can you suggest anything on how to resolve this.
Bhawani SharmaBhawani Sharma
Please update
if(string.valueOf(t.WhoId).startsWith('00Q'))

to

if(t.WhoId != null && string.valueOf(t.WhoId).startsWith('00Q'))