无法使用 jsforce 通过 SalesForce Date 对象查询

Fail to query by a SalesForce Date object using jsforce

我正在使用 jsforce find-api 以按 Date.

类型字段的 DOB 搜索帐户

对 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 and tried the suggestion there, specifically the one in the comment here: 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

但他们也没有帮助

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

终于想通了:

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