4

How would you go about getting the most recent date from a set of records that is less than today?

Example: Record_1 = 1/1/2016 Record_2 = 2/1/2016

If you did a soql like:

Select Id FROM Object Where Date < TODAY

You would get back both of the dates.

How do I get back just the 2/1/2016 date?

1
  • If there are two records with the 2/1/2016 date, would you want to return both of those records, or just one?
    – Derek F
    Jul 11, 2016 at 20:33

1 Answer 1

8

You can add a couple clauses:

SELECT Date__c FROM Object__c
WHERE Date__c < TODAY
ORDER BY Date__c DESC
LIMIT 1
  • The ORDER BY clause makes sure you get the most recent result first.
  • The LIMIT clause makes sure you only get one result.

So if you wanted a method that returns just the Date value it would look like:

public static Date getMostRecentXBeforeToday()
{
    return [/*query*/].Date__c;
}

You must log in to answer this question.

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