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
DbjensenDbjensen 

How to add first record(s) found in soql to list

Hello -I have a soql query that searches for tasks created in the last 7 days that are related to leads created today.

sssTask = [SELECT Id, Owner.Name, WhoId FROM Task 
                       WHERE WhoId != null
                       AND qbdialer__Call_Date_Time__c = Last_N_Days:7
                       AND WhoId IN :leadIds
                       ORDER BY CreatedDate DESC];

A lead may have more than 1 task. I need to add only the most recently created task to a new list for each lead created today. 

List<Task> recentlyCreated = new List<Task>();

Any assistance would be greatly appreciated. 
Best Answer chosen by Dbjensen
PRAKASH JADA 13PRAKASH JADA 13
    public static void checkTransfers(List<Lead> qtFirstTime){

        List<Id> qtFirstTimeIds     = new List<Id>();
        List<Task> taskToBeAdded     = new List<Task>();
        
        map<Id, Task> mapLeadIdandSalesAgnt = new map<Id, Task>();
        
        
        // Loop to iterate over the list of Leads
        for(Lead leadId : qtFirstTime) {
            qtFirstTimeIds.adD(leadId.ID);
        }
        
        // Check on list of Lead Ids
        if(!qtFirstTimeIds.isEmpty()) {
            //search for tasks created last 7 days and Ids in mapLeadIdandSalesAgnt map
           List<Task> tsk = [SELECT Id, WhoId, Owner.Name FROM Task WHERE CreatedDate = Last_N_Days:7
                             AND WhoId = :qtFirstTimeIds
                             ORDER BY CreatedDate DESC];
            
            // Check on the Query results
            if(!tsk.isEmpty()) {
                // Loop to iterate over the query results
                for(Task task : tsk) {
                    
                    // It will add only one task for each Lead.
                    if(task.WhoId != null && !mapLeadIdandSalesAgnt.containsKey(task.WhoId)) {
                        mapLeadIdandSalesAgnt.put(task.WhoId, task);
                    }
                }
            }
            
            // This is to add the tasks.
            if(!mapLeadIdandSalesAgnt.isEmpty()) {
                taskToBeAdded.add(mapLeadIdandSalesAgnt.Values());
            }

        }

    }


Add some debugs to validate that you are getting the right results to the last list that added to capture the task records. 

All Answers

PRAKASH JADA 13PRAKASH JADA 13
create a map with Id, Task and do a loop for the soql results. add then loop over the task records.
add the conditions like

if(WhoId == leadId && !map.containsKey(leadId)) {
 map.add(lead Id, Task);
} else {
//ignore
}

then to your list, you can add map.Values().


in this way you can add the most recent ones.
 
DbjensenDbjensen
Hi Prakash - Thanks for the response. Here's what I'm hoping to accomplish. After I get a list of tasks from leads, I just want to grab each of the most recently created task and add just those records to a single list. Below is simplified code of what I'm trying to do. 
task
DbjensenDbjensen
Where's the code:  public static void checkTransfers(List<Lead> qtFirstTime){

        map<Id, String> mapLeadIdandSalesAgnt = new map<Id, String>();
        //add lead Id and Agent name to map
        for(Lead ld : qtFirstTime){
            mapLeadIdandSalesAgnt.put(ld.Id, ld.Sales_Agent_Name__c);
        }
        
        List<Task> tsk = new List<Task>();
        //search for tasks created last 7 days and Ids in mapLeadIdandSalesAgnt map
        tsk = [SELECT Id, WhoId, Owner.Name FROM Task WHERE CreatedDate = Last_N_Days:7
                    AND WhoId = :mapLeadIdandSalesAgnt.keySet()
                    ORDER BY CreatedDate DESC];
        
        List<Task> tasksFoundList = new List<Task>();
        //Add lead id and owner name to map
        for(Task tksFound : tsk){
            tasksFoundList.add(tksFound);
        }
    }
PRAKASH JADA 13PRAKASH JADA 13
    public static void checkTransfers(List<Lead> qtFirstTime){

        List<Id> qtFirstTimeIds     = new List<Id>();
        List<Task> taskToBeAdded     = new List<Task>();
        
        map<Id, Task> mapLeadIdandSalesAgnt = new map<Id, Task>();
        
        
        // Loop to iterate over the list of Leads
        for(Lead leadId : qtFirstTime) {
            qtFirstTimeIds.adD(leadId.ID);
        }
        
        // Check on list of Lead Ids
        if(!qtFirstTimeIds.isEmpty()) {
            //search for tasks created last 7 days and Ids in mapLeadIdandSalesAgnt map
           List<Task> tsk = [SELECT Id, WhoId, Owner.Name FROM Task WHERE CreatedDate = Last_N_Days:7
                             AND WhoId = :qtFirstTimeIds
                             ORDER BY CreatedDate DESC];
            
            // Check on the Query results
            if(!tsk.isEmpty()) {
                // Loop to iterate over the query results
                for(Task task : tsk) {
                    
                    // It will add only one task for each Lead.
                    if(task.WhoId != null && !mapLeadIdandSalesAgnt.containsKey(task.WhoId)) {
                        mapLeadIdandSalesAgnt.put(task.WhoId, task);
                    }
                }
            }
            
            // This is to add the tasks.
            if(!mapLeadIdandSalesAgnt.isEmpty()) {
                taskToBeAdded.add(mapLeadIdandSalesAgnt.Values());
            }

        }

    }


Add some debugs to validate that you are getting the right results to the last list that added to capture the task records. 
This was selected as the best answer
DbjensenDbjensen
Thanks so much, Prakash. This is working as expected. You rock! Thanks again.