How to make a SOQL query next?
SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO
This causes an error:
MALFORMED_QUERY: Account WHERE LastActivityDate = 30_DAYS_AGO ^
As you do this from the top, you can calculate the date on the top and then bind it to your query, for example.
date d = system.today().addDays(-30); Account [] acc= [select id from account where createdDate = :d];
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
Select Id from Account Where LastActivityDate = N_DAYS_AGO:30
LAST_WEEK and LAST_MONTH also simple and work well.
LAST_WEEK
LAST_MONTH
SELECT id FROM Account WHERE LastActivityDate > LAST_MONTH
For more information, follow this link: http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm
The SOQL date functions page is displayed here: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
To clarify, SOQL allows you to map a date field (for example, LastActivityDate) to a date range using the comparison operator. Therefore, "LastActivityDate = LAST_MONTH" is equivalent to the fact that the date is greater than or equal to the beginning of the first day of the previous month and less than or equal to the end of the last day.