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
Shravan Kumar 71Shravan Kumar 71 

Trigger to Cout the number of existing Task on lead

I have successfully wrote a trigger which calculates all the newly completed activities (Email & Call) and update the total number of a custom filed on the lead object. Similary, I would like to do the same for the completed task which are already completed before the trigger was written however unable to get what the code should be. Below is the trigger that I wrote for the calulcated the New Task :

trigger NoOfEmail on task (after insert, after update) {
    id leadid;
    integer inr = 0;
    List <task> leadTask = new List <task>();
    if (trigger.new!= null) {
        for (task t : Trigger.new) {
            leadid = t.WhoId;
        }
    }
    leadTask = [select id, subject, Status from task where whoid=:leadid];
    for(task t : leadTask){
        if ((t.Subject == 'Email' || t.Subject == 'Call') && t.Status == 'Completed'){
            inr = inr+1;
        }
    }
    
    List <lead> led1 = new list <lead> ();
    List <Lead> led = [select id from lead where id = :leadid];
    for (lead l : led){
        led1.add(l);
        l.Number_of_Activity__c = inr;
    }    
    
    if(led1.size()>0){
        update led1;
    }
}
cvuyyurucvuyyuru
You want to update the lead tasks completed count that were inserted before you developed this trigger.
You need to develop a batch that performs this magic or if don't want to go with that you need to change you trigger to support bulk operations.
cvuyyurucvuyyuru
trigger NoOfEmail on task (after insert, after update) {
    
    set<Id> leadIds = new set<Id>();
    for(task t : trigger.new){
        if ((t.Subject == 'Email' || t.Subject == 'Call') && t.Status == 'Completed'){
           leadIds.add(t.WhoId);
        }
    }
    
    List<lead> lstLeads = new list<Lead>();
    for (lead l : [Select id, (Select id from Tasks where (subject ='Email' or Subject = 'Call') and status = 'Completed') from lead where Id in: leadIds]){
        
        l.Number_of_Activity__c = l.tasks.size();
        lstLeads.add(l);
    }    
    
    if(lstLeads.size()>0){
        update lstLeads;
    }
}

Use this trigger
Shravan Kumar 71Shravan Kumar 71
Hello Charan,

Thank you for your inputs. However the above trigger didn't work as expected. I saved the above trigger in my org, then refreshed the leads which already had completed task, however the Number of Activity filed didn't populate with the total number of closed tasks. I need to manually go and edit on anyone one of the task, post which the filed gets updated. 

The above trigger iterate through new task (Trigger.new), but we need to iterate through the task which are already in the system. As per my understanding we need to use Trigger.oldmap but not able to figure it out how to add in the trigger. 
cvuyyurucvuyyuru
You need to update all the completed tasks in the system not the leads.
Shravan Kumar 71Shravan Kumar 71
I need to count only the total number of already closed tasks on lead. My trigger works on the new closed task but not the tasks which have been already closed before implementing the trigger. 
cvuyyurucvuyyuru
Hi Shravan,
You need to update tasks related to the leads once. When you update, the trigger takes care of the previously created tasks as well.
Do a dummy update on all completed tasks.
you can export through report and update through data loader or dataloader.io
This will be a one time job.