【问题标题】:Fail to query by a SalesForce Date object using jsforce使用 jsforce 无法通过 SalesForce Date 对象查询
【发布时间】:2021-03-24 22:47:01
【问题描述】:

我正在使用 jsforce find-api 来按 DOB 搜索帐户,该帐户是 Date 类型的字段。

对 API 的调用如下所示:

const conn = await connection();
result = await conn.sobject(this.schema.Name).find(rawSObject, Object.keys(this.schema.Fields));

rawSObject 如下所示:

试试 1

const rawSObject = { [AccountFields.PersonBirthdate]: moment(birthDate, 'MM/DD/YYYY').format('YYYY-MM-DD') };

但它失败了:

INVALID_FIELD: 
 PersonBirthdate = '1985-12-12'
                                      ^
ERROR at Row:1:Column:985
value of filter criterion for field 'PersonBirthdate' must be of type date and should not be enclosed in quotes

试试 2

const rawSObject = { [AccountFields.PersonBirthdate]: moment(birthDate, 'MM/DD/YYYY').toISOString();

但仍然出现同样的错误。

试试 3

我也尝试过传递一个日期对象:

const rawSObject = { [AccountFields.PersonBirthdate]: moment(birthDate, 'MM/DD/YYYY').toDate()

这一次我得到了一个不同的错误:

MALFORMED_QUERY: 
PersonBirthdate = Thu Dec 12 1985 00:00:00 GMT+0200
                               ^
ERROR at Row:1:Column:1002
Bind variables only allowed in Apex code
    at HttpApi.getError (/Users/nalfasi/dev/salesforce-gateway/node_modules/jsforce/lib/http-api.js:250:13)...

试试 4

在搜索网络时,我也遇到了以下线程:https://github.com/jsforce/jsforce/issues/851 并尝试了那里的建议,特别是这里评论中的建议:https://github.com/jsforce/jsforce/issues/851#issuecomment-594233217

rawSObject = { [AccountFields.PersonBirthdate]: { $eq: SfDate.toDateTimeLiteral(birthDateObj) };

现在我遇到了一个新错误:

INVALID_FIELD: 
 PersonBirthdate = 1985-12-11T22:00:00Z
                                      ^
ERROR at Row:1:Column:985
value of filter criterion for field 'PersonBirthdate' must be of type date and should not be enclosed in quotes
    at HttpApi.getError (/Users/nalfasi/dev/salesforce-gateway/node_modules/jsforce/lib/http-api.js:250:13)

进一步

我还搜索了网络并发现了以下问题: Cannot Update Custom Date field via SalesForce API (JSforce)

jsforce query method chain to find data between 2 dates

https://salesforce.stackexchange.com/questions/22851/trouble-using-soql-to-filter-results-by-a-range-of-dates-what-is-the-correct-s

https://salesforce.stackexchange.com/questions/8896/using-a-date-for-a-datetime-field-in-a-soql-query

但他们也没有帮助

【问题讨论】:

    标签: javascript node.js date salesforce jsforce


    【解决方案1】:

    错误在于将DateTime 视为Date 对象以及jsforce 文档中缺少示例的组合。

    我终于弄明白了:

    const birthDateObj = moment(birthDate, 'MM/DD/YYYY').toDate();
    rawSObject = { [AccountFields.PersonBirthdate]: { $eq: SfDate.toDateLiteral(birthDateObj) };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多