I'm trying to query Accounts where LastModifiedDate is greater than a given date.
The following code is working fine :
let params = {}
params.LastModifiedDate = { $gte : jsforce.Date.YESTERDAY }
sfConn.sobject('Account')
.find(params)
.execute(async function(err, records) {
if (err) {
return console.error(err)
}
})
but if I pass a js date object like that, I get an error :
let params = {}
params.LastModifiedDate = { $gte : new Date() }
sfConn.sobject('Account')
.find(params)
.execute(async function(err, records) {
if (err) {
return console.error(err)
}
})
the error :
MALFORMED_QUERY:
Account WHERE LastModifiedDate >= Thu Dec 27 2018 13:30:16 GMT+0000
^
ERROR at Row:1:Column:3002
Bind variables only allowed in Apex code
I also tried to pass an ISO formatted date, still giving an error :
let params = {}
params.LastModifiedDate = { $gte : new Date().toISOString() }
sfConn.sobject('Account')
.find(params)
.execute(async function(err, records) {
if (err) {
return console.error(err)
}
})
the error :
INVALID_FIELD:
FROM Account WHERE LastModifiedDate >= '2018-12-27T15:28:16.158Z'
^
ERROR at Row:1:Column:2983
value of filter criterion for field 'LastModifiedDate' must be of type dateTime and should not be enclosed in quotes
Is there a way to query Salesforce for items modified at a specific date ?
It looks like it's a bug in the code generation of the query, the following code is working fine against Salesforce :
sfConn.sobject('Account')
.find('LastModifiedDate >= '+new Date().toISOString())
.execute(async function(err, records) {
if (err) {
return console.error(err)
}
})
I think you have to convert to a proper date, like:
const { SfDate } = require('jsforce');
const dateLiteral = SfDate.toDateLiteral( new Date() )
and then:
params.LastModifiedDate = { $gte : dateLiteral }
toDateLitera() does not work, it has to be toDateTimeLiteral()
e.g.
params.LastModifiedDate = { $gte: SfDate.toDateTimeLiteral('2020-03-03') };
Most helpful comment
toDateLitera()does not work, it has to betoDateTimeLiteral()e.g.
params.LastModifiedDate = { $gte: SfDate.toDateTimeLiteral('2020-03-03') };