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.


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

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>>();
            for(Task taskObj : leadTasks){
                    leadIdVsTaskMap.put(taskObj.Whoid, new List<Task>());   
        for(Lead leadObj : leadList){
                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