+ Start a Discussion
we-mpwe-mp 

Last created record

Hello, 

What criteria can I use in a SOQL query to find the last created record?  For e.g. If I have 3 records created on 4/1, 3/31 and 3/28, the query should return the record created on 4/1.

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

select id, ... from foo_c order by createdDate desc limit 1

All Answers

SuperfellSuperfell

select id, ... from foo_c order by createdDate desc limit 1

This was selected as the best answer
ErikssonEriksson

Hi,

 

It is easy to retrieve the last created record. You can define your SOQL like this:

 

 

List<sObject> records = [select Id from sObject order by CreatedDate DESC limit 1];
if(records.size() > 0)
{
	sObject lastCreated = records[0];
}

 Here the sObject could be any object you want to retrieve.