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
Ben AebischerBen Aebischer 

Activity Count on Leads

Hi, I'm new to coding. However, I modified some existing code from rjpalombo (thanks!) and created a prospecting counter for my leads.
Actually, the counting works - however, when I merge two leads (duplicates) all activities get merged but the counter doesn't get automatically updated. I then either first have to create a new activity or edit an existing activity on the lead, before the counter get updated. Is there a way how I can make sure that this is done automatically? Thank you in advance for your help! : )

Following the Trigger and the Apex Class:

Trigger:
trigger TotalActivityUpdateLeads on Task (after delete, after insert, after undelete, after update) {
//This trigger counts calls made against a Lead
Set<ID> leadIds = new Set<ID>();
//We only care about tasks linked to leads.
String prefix = TotalLeadActivityCount.oppPrefix;
//Add any lead ids coming from the new data
if (Trigger.new != null) {
for (Task t : Trigger.new) {
if (t.WhoId != null && String.valueOf(t.whoId).startsWith(prefix) ) {
leadIds.add(t.whoId);
}
}
}
//Also add any lead ids coming from the old data (deletes, moving an activity from one lead to another)
if (Trigger.old != null) {
for (Task t : Trigger.old) {
if (t.WhoId != null && String.valueOf(t.whoId).startsWith(prefix) ) {
leadIds.add(t.whoId);
}
}
}
if (leadIds.size() > 0)
TotalLeadActivityCount.updateLeadCounts(leadIds);
}

Apex Class:
public with sharing class TotalLeadActivityCount {
public static Boolean didRun = false;
public static String oppPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
/*
* Takes a set of opportunity IDs, queries those leads, and updates the activity count if appropriate
*/
public static void updateLeadCounts(Set<ID> leadIds) {
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> leads = [SELECT ID, Total_Lead_Activity_Count__c, (SELECT ID FROM Tasks where (RecordTypeId='01260000000JNHF' OR RecordTypeId='01260000000JMaa' OR RecordTypeId='01260000000JESu') AND Status='Completed'), (SELECT ID FROM Events) FROM Lead WHERE ID IN :leadIds];
List<Lead> updateLead = new List<Lead>();
for (Lead l : leads) {
Integer count = l.tasks.size();
if (l.Total_Lead_Activity_Count__c != count) {
l.Total_Lead_Activity_Count__c = count;
updatelead.add(l); //we're only doing updates to leads that have changed...no need to modify the others
}
}
//Update the appropriate lead
try {
update updateLead;
} catch (Exception e) {
//This is controversial. Anything could happen when updating the opportunity..validation rule, security, etc. The key is we don't
//want the event update to fail...so we put a try catch around the opp update to make sure we don't stop that from happening.
}
}
}
}