function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bohemianguy100bohemianguy100 

Date comparision: find the most recent date

I'm trying to compare dates to determine the most recent date out of several records.  I was to get the record with the most recent date below, but that doesn't appear to be working.

 

tmpPostedDate = null
postedDate = null;

for (Sales_Invoice__c s : salesInvoiceList) {
                tmpPostedDate = s.Posting_Date__c;
                if (tmpPostedDate > postedDate) {
                                postedDate = tmpPostedDate;
                }
                else {
                                s.Posting_Date__c;
                }
}

Is there another way to get the most recent date when comparing across several records?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
MaxiMizeMaxiMize

I stole this from another post, but it should work... Since SOQL doesn't have a MAX() function, you have to sort on date, and limit returns to 1.

 

 

SELECT PSOD_Project__r.Name,Resource__r.Name, Period_Beginning__c
FROM Time_Sheet__c
WHERE PSOD_Project__r.Name = 'PSOD Internal'
ORDER BY Period_Beginning__c DESC
LIMIT 1

 

 

If you want to read the post on this subject, it's located here:

 

Order By Date discussion