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
Anu Raj.ax1269Anu Raj.ax1269 

How to get the Latest Task created on Lead

I am trying to get the latest task created on Lead from task object.

My scenario is that if there are 2 leads - Lead1 and Lead2. And both have 2 task created for it, Lead1 contain taska - (created date = today) taskb - (created date = yesterday). And for Lead2 contain taskc - (created date = today - 2) taskd - (created date = today - 4).

It should give result of all the latest created task in the lead1 and lead2 (Result - lead1 (taska) And Lead2(taskc)).

Please provide me the possible ways to do this scenario.

SimonJaiSimonJai

Maybe others have a better way, but personally I would use SOQL to find the latest task for each lead.

 

Lead leadToLookup = new Lead();

Task latestTask = [SELECT Id FROM Task WHERE WhoId =: leadToLookup.Id AND isDeleted = false ORDER BY CreatedDate DESC LIMIT 1];

 

SurekaSureka
Hi,

Try this:

accLst = [Select Id,Name,(Select Id,createdDate, Subject From Tasks order by CreatedDate Limit 1) From Account];
for(Account a: accLst)
{
for(Task t: accLst.Tasks)
{
tskLst.add(t);
}
}

Thanks
MoggyMoggy

I guess on the above code you will need to change Account to Lead

same in the for statement