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