You need to sign in to do that
Don't have an account?
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;
}
}
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;
}
}
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.
Use this trigger
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.
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.