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
KVaishnaKVaishna 

Records created in last 24 hours

Hi,

 

I want to get records created in last 24 hours in Apex SoQL query like

 

'Select Id, name, recordtypeid from Opportunity where createddate = LAST 24 Hours'. How can I do that?

I can get it using system.now() - 1 but SoQL query does not support returned datetime format.

 

Thanks

- Kunjan

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

 

datetime dt = System.now()-1;
List<Opportunity> ops = [select id from opportunity where createdDate > :dt];

 

datetime dt = System.now()-1;

List<Opportunity> ops = [select id from opportunity where createdDate > :dt];

All Answers

BenjiJasikBenjiJasik

How about using the date literals?

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_dateformats.htm

 

where createddate > LAST_N_DAYS:1 

 

You could always calculate the exact date string and use that too

KVaishnaKVaishna

Thanks for the reply.

 

LAST_N_DAYS:1 would give me from 12 AM to 11:59 PM of that particular day. It would not give me specific last  24 hour range at any time in the day.

SuperfellSuperfell

 

datetime dt = System.now()-1;
List<Opportunity> ops = [select id from opportunity where createdDate > :dt];

 

datetime dt = System.now()-1;

List<Opportunity> ops = [select id from opportunity where createdDate > :dt];

This was selected as the best answer
KVaishnaKVaishna

Thanks SimonF.

rohitrrohitr

What if i need this in an SOQL query for DataLoader?

 

WHERE CreatedDate > LAST_1_HOUR