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
Ashu sharma 38Ashu sharma 38 

How to get number of task attached in past time in lead object.

Hi,

Is there any way,how to get the counts for task attached in lead object in past times.

Thanks.
sachinarorasfsachinarorasf
Hi Ashu,

Please use the below code to get past days tasks related to Lead.

public class getPastTimeTaskRelLead {
    public static void getAllPastTimeLead(){
        Lead[] leadList = [SELECT id, Name from Lead LIMIT 50000];
        Task[] leadTasks = [SELECT id, subject, Whoid, LastModifiedDate FROM Task WHERE Whoid in: leadList AND LastModifiedDate = TODAY LIMIT 50000];
        System.debug('leadTasks:' + leadTasks);
        Map<Id, List<Task>> leadIdVsTaskMap = new Map<Id, List<Task>>();
        if(leadTasks.size()>0){
            for(Task taskObj : leadTasks){
                if(!leadIdVsTaskMap.containsKey(taskObj.Whoid)){
                    leadIdVsTaskMap.put(taskObj.Whoid, new List<Task>());   
                }
                leadIdVsTaskMap.get(taskObj.Whoid).add(taskObj);
            }
        }
        for(Lead leadObj : leadList){
            if(leadIdVsTaskMap.containsKey(leadObj.Id)){
                List<Task> getTaskList = leadIdVsTaskMap.get(leadObj.Id);
                System.debug('leadObj Name::::'+leadObj.Name+'----related number of task in past days::::'+getTaskList.size());
            }
        }
        
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com