+ Start a Discussion
Andrii VolkovAndrii Volkov 

Apex Trigger to Count Activities Related to Current Lead Owner

Lowly Admin here who doesn't know Apex... I need to count the number of Activities (Tasks and Events) associated with the Lead that were logged by CURRENT lead owner. I want the total to appear in a field on the Lead object called Times_contacted_by_owner. Would any of you gracious developers be willing to help me with the trigger?
Best Answer chosen by Andrii Volkov
David Zhu 🔥David Zhu 🔥
You can refer  the following two triggers:
trigger TaskTrigger on Task (after insert,after delete) {

    List<Id> leadIds = new List<Id>();
    if (trigger.isInsert) {
        for (Task task : trigger.new){
            if (task.whoId !=null && string.valueOf(task.whoId).startsWith('00Q')) {
                leadIds.add(task.whoId);
            }
        }
    } else if (trigger.isDelete) {
        for (Task task : trigger.old){
            if (task.whoId !=null && string.valueOf(task.whoId).startsWith('00Q')) {
                leadIds.add(task.whoId);
            }
        }
    }
    
    
    if (leadIds.size() > 0){

        Map<Id,Lead> leadMap = new Map<Id,Lead>();
        
        List<Lead> leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from events) from lead where Id in :leadIds];
        
        for (Lead lead : leads) {
            Integer count = 0;

            for (Event event : lead.events) {
                if (event.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            
            lead.Times_contacted_by_owner__c = count;
            leadMap.put(lead.Id,lead);
        }

        leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from tasks) from lead where Id in :leadIds];
        for (Lead lead : leads) {
            Integer count = 0;
            for (Task task : lead.tasks) {
                if (task.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            if (leadMap.containsKey(lead.Id))
            {
                Lead l = leadMap.get(lead.Id);
                l.Times_contacted_by_owner__c += count;
                leadMap.put(lead.Id,l);
            }
            else
            {
                lead.Times_contacted_by_owner__c = count;
                leadMap.put(lead.Id,lead);
            }
        }
        update leadMap.values();
    }
}
 
trigger EventTrigger on Event (after insert,after delete) {
    List<Id> leadIds = new List<Id>();
    if (trigger.isInsert) {
        for (Event event : trigger.new){
            if (event.whoId !=null && string.valueOf(event.whoId).startsWith('00Q')) {
                leadIds.add(event.whoId);
            }
        }
    } else if (trigger.isDelete) {
        for (Event event : trigger.old){
            if (event.whoId !=null && string.valueOf(event.whoId).startsWith('00Q')) {
                leadIds.add(event.whoId);
            }
        }
    }
    
    
    if (leadIds.size() > 0){

		Map<Id,Lead> leadMap = new Map<Id,Lead>();
        
        List<Lead> leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from events) from lead where Id in :leadIds];
        
        for (Lead lead : leads) {
            Integer count = 0;

            for (Event event : lead.events) {
                if (event.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            
            lead.Times_contacted_by_owner__c = count;
            leadMap.put(lead.Id,lead);
        }

        leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from tasks) from lead where Id in :leadIds];
        for (Lead lead : leads) {
            Integer count = 0;
            for (Task task : lead.tasks) {
                if (task.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            if (leadMap.containsKey(lead.Id))
            {
                Lead l = leadMap.get(lead.Id);
                l.Times_contacted_by_owner__c += count;
                leadMap.put(lead.Id,l);
            }
            else
            {
                lead.Times_contacted_by_owner__c = count;
                leadMap.put(lead.Id,lead);
            }
        }
        update leadMap.values();
    }
    
}

 

All Answers

VinayVinay (Salesforce Developers) 
Hi Andri,

Below link has a working examples

https://developer.salesforce.com/forums/?id=906F00000009sTZIAY
https://salesforce.stackexchange.com/questions/213549/help-with-a-trigger-for-counting-activities

However, you would need a developer to make changes as per your requirement in your org.

Thanks,
Vinay Kumar
David Zhu 🔥David Zhu 🔥
You can refer  the following two triggers:
trigger TaskTrigger on Task (after insert,after delete) {

    List<Id> leadIds = new List<Id>();
    if (trigger.isInsert) {
        for (Task task : trigger.new){
            if (task.whoId !=null && string.valueOf(task.whoId).startsWith('00Q')) {
                leadIds.add(task.whoId);
            }
        }
    } else if (trigger.isDelete) {
        for (Task task : trigger.old){
            if (task.whoId !=null && string.valueOf(task.whoId).startsWith('00Q')) {
                leadIds.add(task.whoId);
            }
        }
    }
    
    
    if (leadIds.size() > 0){

        Map<Id,Lead> leadMap = new Map<Id,Lead>();
        
        List<Lead> leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from events) from lead where Id in :leadIds];
        
        for (Lead lead : leads) {
            Integer count = 0;

            for (Event event : lead.events) {
                if (event.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            
            lead.Times_contacted_by_owner__c = count;
            leadMap.put(lead.Id,lead);
        }

        leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from tasks) from lead where Id in :leadIds];
        for (Lead lead : leads) {
            Integer count = 0;
            for (Task task : lead.tasks) {
                if (task.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            if (leadMap.containsKey(lead.Id))
            {
                Lead l = leadMap.get(lead.Id);
                l.Times_contacted_by_owner__c += count;
                leadMap.put(lead.Id,l);
            }
            else
            {
                lead.Times_contacted_by_owner__c = count;
                leadMap.put(lead.Id,lead);
            }
        }
        update leadMap.values();
    }
}
 
trigger EventTrigger on Event (after insert,after delete) {
    List<Id> leadIds = new List<Id>();
    if (trigger.isInsert) {
        for (Event event : trigger.new){
            if (event.whoId !=null && string.valueOf(event.whoId).startsWith('00Q')) {
                leadIds.add(event.whoId);
            }
        }
    } else if (trigger.isDelete) {
        for (Event event : trigger.old){
            if (event.whoId !=null && string.valueOf(event.whoId).startsWith('00Q')) {
                leadIds.add(event.whoId);
            }
        }
    }
    
    
    if (leadIds.size() > 0){

		Map<Id,Lead> leadMap = new Map<Id,Lead>();
        
        List<Lead> leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from events) from lead where Id in :leadIds];
        
        for (Lead lead : leads) {
            Integer count = 0;

            for (Event event : lead.events) {
                if (event.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            
            lead.Times_contacted_by_owner__c = count;
            leadMap.put(lead.Id,lead);
        }

        leads = [select id, Times_contacted_by_owner__c,ownerId,(select id,ownerId,subject from tasks) from lead where Id in :leadIds];
        for (Lead lead : leads) {
            Integer count = 0;
            for (Task task : lead.tasks) {
                if (task.ownerId == lead.OwnerId) {
                    count ++;
                }
            }
            if (leadMap.containsKey(lead.Id))
            {
                Lead l = leadMap.get(lead.Id);
                l.Times_contacted_by_owner__c += count;
                leadMap.put(lead.Id,l);
            }
            else
            {
                lead.Times_contacted_by_owner__c = count;
                leadMap.put(lead.Id,lead);
            }
        }
        update leadMap.values();
    }
    
}

 
This was selected as the best answer
Andrii VolkovAndrii Volkov
Thank you, David Zhu! That works in Sandbox Env as expected. Would you be so kind to assist with tests as well?