There are 2 fields in the lead object ,total_count__c is for count the total no of task on the lead and in the No_of_Call_Count__c i want only those task whose subject contains 'Call' in the subject name i want total no task count in this field total_count__c and those task subject contain 'call' in this field No_of_Call_Count__c but its only displaying total no of task count on both fields

public void updateActivityCountsOnLead(List<Task> tasklist){
    List<Task> finaltasklist= new List<Task>();
    List<Task> finaltaskCalllist= new List<Task>();
    Set<Id> leadidset= new Set<Id>();
    Set<Id> leadidsetCount= new Set<Id>();
    List<Lead> leadlistupdate= new List<Lead>();
    for(Task task:tasklist)
    if(task.WhoId!=null && task.WhoId.getSObjectType().getDescribe().getName()=='Lead')
        if(task.WhoId!=null && task.WhoId.getSObjectType().getDescribe().getName()=='Lead'
        && task.status=='Completed' && (task.subject.contains('Call')
    AggregateResult[] arList=[select WhoId,count(id) countid from task where WhoId in :leadidset or WhoId in :leadidsetCount 
                              group by WhoId ];
    for(AggregateResult ar:arList){
        Lead lead = new Lead();
        lead.No_of_Call_Count__c =String.valueOf(ar.get('countid'));
        update leadlistupdate;
  • Please remove tags unrelated to your question, e.g. governorlimits. Sep 26, 2022 at 6:23

1 Answer 1


To get the exact count you can try writing a trigger for all the three cases. After insert, after update and after delete. So that if a task is created under a lead or updated/deleted under lead it will update the count accordingly.

Example: Let say we have A and B as leads and we have 3 tasks each. If we have created a new task A1 under A then count will be 4 for A. Let's say we have mapped the id of A1 task to B then it should update count as 3 for A and 4 for B. Also if we delete a task then the count should be updated respectively.

So to cover all these scenarios we can use trigger contexts like trigger.new and trigger.old.

Let me know if you were able to achieve with this. Else i can help in trigger.

  • no there are only two cases 1. Total count 2.call count ,actually the problrm is if i used two queries on same methods its throwing soql 101 limit exceed error ,so i need to merege that two cases in one @Santhosh
    – user122731
    Sep 26, 2022 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .