I'm not finding a way to get the latest updates Scenario :

There are three people which is assigned to a given lead.

Lead name : L1

User Name is John,Adam,Charles

Created Date    NewValue    OldValue    Lead
01/03/2014  Adam    John    L1
03/03/2014  Charles Adam    L1
07/03/2014  John    Charles L1

How do we fire a soql on L1's leadhistory object so that we get only below result

07/03/2014  John    Charles 

Please help

  • possible duplicate of SOQL on LeadHistory object Mar 6 '14 at 12:56
  • Thanks Samuel. This is different scenario now As that didn't fulfill the case and still stuck.
    – JJoseph
    Mar 6 '14 at 12:59
  • It looked like a different version of the same question to me. apologies. I have re-tracted my close vote. Mar 6 '14 at 13:05
  • Yes its the same version but this scenario is more specific and wanted to say the same thing in previous one. May be you have taken it in different way. Please suggest the efficient way to get a query. Thanks
    – JJoseph
    Mar 6 '14 at 13:10
  • The example in this question did help, see my answer. Mar 6 '14 at 13:12

You can use a combination of ORDER BY and LIMIT on your SOQL query to get that result.

Not much different than your other question on how to query lead history

SELECT CreatedById,CreatedDate,Field,Id,LeadId,NewValue, oldvalue 
FROM LeadHistory 
WHERE field = 'Owner' AND LieadId ='XXXXXXXXXX'
  • Thanks Samuel. But here I don't want to use LIMIIT.Is there any other way to get the same result ?
    – JJoseph
    Mar 6 '14 at 13:12
  • Can you explain why you do not want to use limit ? (Because you want to query for a bulk amount of leads at the same time ?) Mar 6 '14 at 13:13
  • Thanks again Samuel .I did the same query with the LIMIT. But i'm looking for a more efficient query. Is there any other way of getting same result . Help again will be appreciated.
    – JJoseph
    Mar 6 '14 at 13:18
  • You have to define what you consider "efficient" and what your application context and scenario is. It is very hard to give you the answer to your true question, only knowing half the question. Mar 6 '14 at 13:25
  • It does give me the latest one. Working on 4 leads only and want to know if there is other way like using aggregate functions like MAX or MIN ?
    – JJoseph
    Mar 6 '14 at 13:36

This query should get only the latest one:

SELECT CreatedById,CreatedDate,Field,Id,LeadId,NewValue, oldvalue 
    FROM LeadHistory 
    WHERE field = 'Owner' AND  LeadID='yourleadid' ORDER BY CreatedDate DESC LIMIT 1 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.