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
Yogendra RishishwarYogendra Rishishwar 

how to get current Date in SOQL

How to get current Date time in Salesforce Object Query Language .
Suppose I want to get all cases which have been closed in last 30 days then what should be query .

Select CaseNumber from Case where ClosedDate >= CURRENT_DATE - 30 Days ;
 
What date function will return cirrent date in SOQL?

Thanks
Yogendra Rishishwar
 
Best Answer chosen by Yogendra Rishishwar
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to see all SOQL date method in salesforce with example i hope that will help you
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_date_functions.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

You can try LAST_N_DAYS:n
Select CaseNumber from Case where ClosedDate = LAST_N_DAYS:30
All below fucntion are helpfull for you
LAST_MONTH
LAST_90_DAYS
LAST_N_DAYS:n

Please let us know if this will help you

Thanks
Amit Chaudhary


 

All Answers

Adam OlshanskyAdam Olshansky
If you're looking for Datetime, use Datetime.now() but if you're looking for Date, use Date.today()
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm
Paul_BoikoPaul_Boiko
In SOQL you can use TODAY to get current date.
Here you can see Date Literals for SOQL:https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to see all SOQL date method in salesforce with example i hope that will help you
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_date_functions.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

You can try LAST_N_DAYS:n
Select CaseNumber from Case where ClosedDate = LAST_N_DAYS:30
All below fucntion are helpfull for you
LAST_MONTH
LAST_90_DAYS
LAST_N_DAYS:n

Please let us know if this will help you

Thanks
Amit Chaudhary


 
This was selected as the best answer
Yogendra RishishwarYogendra Rishishwar
Thanks Adam, Paul, Amit
My Problem got resolved,
Thanks again.
 
Lufuno Mbulungeni 1Lufuno Mbulungeni 1
How can i get data that was created 5 or 10 minutes ago ? Below get's me all records which is what i don't want 
SELECT Id, CreatedDate, Destination_System__c, Method_Name__c, Request_Timestamp__c, Response_Timestamp__c, Response__c, Source_System__c, Status_Code__c, Status__c, User_Name__c
FROM Sys_Log_Integration__c WHERE CreatedDate = TODAY
Adam OlshanskyAdam Olshansky
This answer may help you https://developer.salesforce.com/forums/?id=906F0000000MNb3IAG
suwarna rao 14suwarna rao 14
That was very helpfull, I also want to querry cases =Today()+7 . Is there any function like LAST_N_DAYS:n for future?