Jsforce: How to query items modified after a given date ?

Created on 27 Dec 2018  路  3Comments  路  Source: jsforce/jsforce

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 ?

Most helpful comment

toDateLitera() does not work, it has to be toDateTimeLiteral()

e.g.
params.LastModifiedDate = { $gte: SfDate.toDateTimeLiteral('2020-03-03') };

All 3 comments

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') };

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbonigomes picture jbonigomes  路  3Comments

stomita picture stomita  路  5Comments

freshlogic picture freshlogic  路  8Comments

MoshikEilon picture MoshikEilon  路  3Comments

akatechis picture akatechis  路  4Comments