8

如何进行这样的 SOQL 查询?

SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO

这会产生一个错误:

MALFORMED_QUERY: 
Account WHERE LastActivityDate = 30_DAYS_AGO
                      ^
4

7 回答 7

18
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
于 2011-03-28T21:11:08.293 回答
15

当您从 apex 执行此操作时,您可以计算 apex 中的日期,然后将其绑定到您的查询中,例如

date d = system.today().addDays(-30);
Account [] acc=  [select id from account where createdDate = :d];
于 2011-03-08T23:40:44.960 回答
9
Select Id from Account Where LastActivityDate = N_DAYS_AGO:30
于 2012-07-11T18:51:33.850 回答
1

LAST_WEEK并且LAST_MONTH也很容易并且工作良好。

SELECT id FROM Account WHERE LastActivityDate > LAST_MONTH

有关更多数据,请查看此链接: http: //www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm

于 2015-04-04T13:50:58.010 回答
0

由于它是 30 天前,你可以使用这个 -

SELECT ID FROM Account WHERE LastActivityDate < LAST_N_DAYS:30
于 2018-10-04T16:06:37.920 回答
0

您的查询时间段位于提供 SFDC 最好使用它的日期文字中,因为指定的时间是一个广泛的数字,您只需要提供天数并相应地使用运算符,即 '=' ,'>' 或'<'

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

如果您只提供它所在的天数/月数,您的查询看起来会更简单。

SELECT id FROM Account WHERE LastActivityDate = LAST_N_MONTHS:1

或者

SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30

谢谢,OQ。

于 2021-09-22T07:27:50.753 回答
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.

于 2015-09-17T02:37:05.057 回答