8

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
                      ^
3
  • Are you executing this query in a web service call or in Apex code? superfell's answer is correct if this is in Apex. Commented Mar 9, 2011 at 19:22
  • Via a webservice, but I just declared the date in the program and passed it as a string to the query as a specific date.
    – Dave S
    Commented Mar 9, 2011 at 20:23
  • 2
    I assumed you were using apex, as you tagged the question with apex.
    – superfell
    Commented Mar 10, 2011 at 6:54

7 Answers 7

18
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
1
  • 1
    This would be inclusive of all 30 days, OP only wanted the last activity from one day.
    – willard
    Commented Feb 9, 2023 at 19:44
16

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];
1
  • LAST_N_DAYS would be a better approach since SOQL already has a syntax to do this, or maybe they added it later? Commented Oct 4, 2018 at 16:07
9
Select Id from Account Where LastActivityDate = N_DAYS_AGO:30
0
1

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

0

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.

0

Since it is 30 days ago, you an use this -

SELECT ID FROM Account WHERE LastActivityDate < LAST_N_DAYS:30
2
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.