The above query results in an error

SELECT MAX(CreatedDate),OldValue,NewValue 
FROM LeadHistory 
GROUP BY OldValue,NewValue

The error :

ERROR at Row:1:Column:69 field 'OldValue' can not be grouped in a query call

Can someone explain this error ?


If you execute this code in "Developer Console"/"Debug"/"Open Execute Anonymous Window":

System.debug('>>> isGroupable=' + LeadHistory.OldValue.getDescribe().isGroupable());

you will see that false is returned and the documentation says:

isGroupable() Returns true if the field can be included in the GROUP BY clause of a SOQL query, false otherwise

So this is a limitation of the platform, and you will have to query the values ungrouped and use Apex code to achieve your desired logic instead.

|improve this answer|||||
  • Thanks Kieth for your valuable information. I have one scenario where – JJoseph Mar 6 '14 at 12:08
  • I want to get only the latest update about owner change on particular lead record by querying leadhistory object.my query is select createdate, oldvalue, newvalue from leadhistory where leadid='0145545xyz' .This will return all the records from created date to new date. – JJoseph Mar 6 '14 at 12:09
  • I will get my desired result by using order by on created date with LIMIT=1 . But I want it in efficient way. Please help. – JJoseph Mar 6 '14 at 12:16
  • Adding something like "Field = 'Owner'" to the where and adding "order by CreatedDate desc" should do it. But if you run into problems I suggest a new question. Also please accept the above answer as what we are discussing here is beyond the scope of your question. – Keith C Mar 6 '14 at 12:37

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.