在特定的日期时间之后,如何在Python脚本中过滤SOQL查询?

问题描述

我试图从OPPORTUNITY对象中选择所有大于变量DateTime的记录。但是,我不知道如何。这是使用simple_salesforce软件包的Python脚本。我认为问题是我从BigQuery表中检索的“ max_date”参数中缺少毫秒和+0000时区规范,或者没有将max_date参数作为正确的数据类型传递。>

我的示例代码无效:

max_date = '2020-08-11T17:41:29'

SF_QUERY = ("""
  SELECT Id,CreatedDate
  FROM Opportunity
  WHERE CreatedDate > %s
  """ % max_date)

CreatedDate字段的格式如下:

2019-10-31T16:01:19.000+0000

查询返回错误

 Response content: [{'message': "line 8:57 no viable alternative at character '<EOF>'",'errorCode': 'MALFORMED_QUERY'}]

如果我在%s周围添加引号,使其成为“%s”,则会出现错误

Response content: [{'message': "\n  AND BU_Last_Stage_Changed_Date__c >\n      ^\nERROR at Row:8:Column:7\nvalue of filter criterion for field 'BU_Last_Stage_Changed_Date__c' must be of type dateTime and should not be enclosed in quotes",'errorCode': 'INVALID_FIELD'}]

感谢您的帮助。

参考文档:

解决方法

解决方案是在我的datetime字符串的末尾附加一个表示ZUT时间(表示UTC)的“ Z”。动态变量%s不应包含引号。示例:

max_date = '2020-08-10T17:41:29' # in prod,pull this data from BigQuery
max_date = max_date + "Z"

SF_QUERY = ("""
  SELECT Id,CreatedDate
  FROM Opportunity
  WHERE CreatedDate > %s
  """ % max_date)