Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a SObject called DDStatus__c which has LastUpdated__c field (DateTime type). How can I get latest datetime using SOQL query.

share|improve this question
2  
Per record or overall MAX as an aggregate result ? –  techtrekker Jan 4 '13 at 11:17
add comment

2 Answers 2

up vote 2 down vote accepted

You should be able to do this query to get the record showing the latest datetime:

[Select Id From DDStatus_c Order By LastUpdated_c DESC Limit 1]

You can add a Where clause if this is a child object and you're looking for the latest datetime for a specific parent record.

share|improve this answer
    
[Select Id From DDStatus_c Order By LastUpdated_c DESC Limit 1] is returning list. why? –  Bhushan Lodha Jan 4 '13 at 18:33
1  
Any SOQL query statement will return a list. But there are few exceptions like the COUNT aggregate query that returns an integer. You can get the first element from the list returned by the query by checking the list count. –  Vijay Prakash Jan 5 '13 at 15:55
add comment

Unless I'm missing something, this line should work, as long as you are certain the record you're querying for exists. If you aren't sure it exists, you should cast the query to a list, and look for the first element in the list to get the field value from.

Datetime dt = [select LastUpdated__c from DDStatus__c where Id = :someId].LastUpdated__c;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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