Search code examples
salesforceapex-codesoql

SOQL query WHERE Date = 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
                      ^

Solution

  • 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];