You need to sign in to do that
Don't have an account?
Count Activities on Leads
Found this code to count activities on Opportunities. I've tried to re-purpose it for Leads. It's not passing the tests. The error says System.AssertException: Assertion Failed: Expected: 1, Actual: nullClass.LeadActivityCount.testCountTask: line 52, column 1
Here's the code:
public with sharing class LeadActivityCount {
public static Boolean didRun = false;
public static String ldPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
/*
* Takes a set of Lead IDs, queries those leads, and updates the activity count if appropriate
*/
public static void updateLeadCounts(Set<ID> ldIds) {
if (didRun == false) { //We only want this operation to run once per transaction.
didRun = true;
//Query all the leads, including the tasks child relationships
List<Lead> ld = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Lead WHERE ID IN :ldIds];
List<Lead> updateld = new List<Lead>();
for (Lead l : ld) {
Integer count = l.tasks.size() + l.events.size();
if (l.activity_count__c != count) {
l.activity_count__c = count;
updateld.add(l); //we're only doing updates to ld that have changed...no need to modify the others
}
}
//Update the appropriate leads
try {
update updateld;
} catch (Exception e) {
//This is controversial. Anything could happen when updating the Lead..validation rule, security, etc. The key is we don't
//want the event update to fail...so we put a try catch around the lead update to make sure we don't stop that from happening.
}
}
}
/*
* Test method for this class and TaskUpdateLead and EventUpdateLead
*/
public static testMethod void testCountTask() {
//Setup
Lead l = new Lead(lastname='Test', company='Test Company');
insert l;
//Insert our first task
Task t = new Task(subject='Test Activity', whoId = l.id);
insert t;
//Verify count
l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
System.assertEquals(1,l.activity_count__c);
//Disconnect task from the Lead
didRun = false; //Reset
t.whatId = null;
update t;
//Verify count = 0
l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
System.assertEquals(0,l.activity_count__c);
didRun = false; //Reset
//Add an event
Event e = new Event(subject='Test Event', whoId = l.id, startDateTime = System.Now(), endDateTime = System.now());
insert e;
//Verify count = 1
l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
System.assertEquals(1,l.activity_count__c);
//Relink the task to the Lead
didRun = false; //Reset
t.whatId = l.id;
update t;
//Verify count = 2
l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
System.assertEquals(2,l.activity_count__c);
//Disconnect the event from the Lead
didRun = false; //Reset
e.whatId = null;
update e;
//Verify count is back down to 1
l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
System.assertEquals(1,l.activity_count__c);
//Delete the task
didRun = false; //reset
delete t;
//Verify count is back down to 0
l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
System.assertEquals(0,l.activity_count__c);
}
}
Realizing I had WhatID instead of WhoID. I fixed those, but I still have the same error.
The unit test is not calling the class method, so nothing is being updated.