How do I make a SOQL query like this?
SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO
This produces an error:
MALFORMED_QUERY:
Account WHERE LastActivityDate = 30_DAYS_AGO
^
How do I make a SOQL query like this?
SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO
This produces an error:
MALFORMED_QUERY:
Account WHERE LastActivityDate = 30_DAYS_AGO
^
As you're doing this from apex, you can calculate the date in apex, then bind that into your query, e.g.
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
As you're doing this from apex, you can calculate the date in apex, then bind that into your query, e.g.
date d = system.today().addDays(-30);
Account [] acc= [select id from account where createdDate = :d];
Select Id from Account Where LastActivityDate = N_DAYS_AGO:30
LAST_WEEK
and LAST_MONTH
are also easy and work well.
SELECT id FROM Account WHERE LastActivityDate > LAST_MONTH
For more data look at this link: http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm
The page of SOQL date functions appears to have moved 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 a date field (e.g. LastActivityDate) to be compared against a range of dates using a comparison operator. So "LastActivityDate = LAST_MONTH" is equivalent to saying that the date is greater than or equal to the start of the first day of the previous month AND less than or equal to the end of the last day.
Since it is 30 days ago, you an use this -
SELECT ID FROM Account WHERE LastActivityDate < LAST_N_DAYS:30
Your query time period lies in the Date literals provided SFDC its best to use it as the time specified is a broad number , , you just need to provide the no of days and accordingly use the operator which is '=' ,'>' or '<'
LAST_N_DAYS:n LAST_N_WEEKS:n LAST_N_MONTHS:n LAST_N_YEAR:n
NEXT_N_DAYS:n NEXT_N_WEEKS:n NEXT_N_MONTHS:n NEXT_N_YEAR:n
your query would look simpler if you just provided the no of days / month which it falls in .
SELECT id FROM Account WHERE LastActivityDate = LAST_N_MONTHS:1
or
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
Thanks, OQ.
© 2022 - 2024 — McMap. All rights reserved.