0

I have a requirement to get the most recent task date on an account. When I use the following query, Salesforce does not return the correct answer. If I do the same query but on the task object, the result is correct.

SELECT ...
(
    SELECT Id, ActivityDate, AccountId, WhatId, WhoId From Tasks
    Where SalesLoft1__Call_Disposition__c = null
    and (Not subject Like '%aircall%')
    and (Not subject Like '%Marketing%')
    and ActivityDate != null order by ActivityDate desc
) from Account where Id = '0015q000006T3y3AAC'

The result from this gives me an older task

enter image description here

When I query from the Task object, I get the correct task. Why would there be different results for the same account?

SELECT Id, subject, ActivityDate, AccountId, WhatId, WhoId From Task
Where accountid = '0015q000006T3y3AAC'
and SalesLoft1__Call_Disposition__c = null
and (Not subject Like '%aircall%')
and (Not subject Like '%Marketing%')
and ActivityDate != null order by ActivityDate desc Limit 1

enter image description here

Any ideas?

2 Answers 2

0

The Tasks relationship is tied to the WhatId field. If you look closer, you'll see in the first screenshot that WhatId is populated with the Account Id, while in the second query, you'll see that WhatId is null (not populated). In other words, the second query is returning a task that is indirectly related to the Account (e.g. a Contact or Opportunity), but is not directly related to the Account. I'll leave it up to you to decide which task you really meant to retrieve, as they are two different concepts.

2
  • Thanks @sfdcfox. I would like to fetch the most recent task in account it doesn't matter whether directly or indirectly related. How would I do that?
    – Pauline
    May 4, 2023 at 11:13
  • @Pauline You can query from ActivityHistories or OpenActivities, depending on which you're looking for, or both, and just take the highest activity date of either.
    – sfdcfox
    May 4, 2023 at 12:13
0

You can get the result by appending a clause called MRU. However, you cannot use additional filters with it. enter image description here

I have used your query but I guess you want the reverse of it. The thing is you can either query recent accounts or tasks. may be not both... However MRU works

MRU means MOST RECENTLY USED I guess

1
  • Is this an answer to this particular question?
    – Derek F
    May 10, 2023 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .