Cannot Update Custom Date field via SalesForce API (JSforce)

950 Views Asked by At

I'm using the JSforce library from a Node.js app to update a custom date field on an Opportunity object, but the field is not being updated. The response indicates that the update is successful and the SystemModstamp field is updated on the object, however the field remains null.

This is the JSforce call I'm using:

conn.sobject("Opportunity").update({
    Id: opportunityId,
    Contract_Cancelled_Date__c: new Date("2018-09-13")
}, function(err, ret) {
    if (err) {
        console.log(err);
    } else {
        console.log(ret);
    }
});

I've tried converting the date to a string: (new Date("2018-09-13")).toIsoString(). I've also tried passing in a string constant: "2018-09-13T00:00:00.000Z". All result in the same outcome.

I'm able to update other non-date custom fields without issue and I'm able to update the non-custom date field CloseDate successfully.

2

There are 2 best solutions below

0
On

You can use inbuilt classes provided by jsforce

for example

const { SfDate }   = require('jsforce');

// use toDateLiteral when the field is just Date type
const dateLiteral = SfDate.toDateLiteral( new Date() )

// use toDateTimeLiteral when the field is just Date/Time type
const dateLiteral = SfDate.toDateTimeLiteral(new Date())
0
On

I had a similar problem (I used bulkApi). I was able to solve the problem using the following 'YYYY-MM-DD'. Try using something like moment(date).format('YYYY-MM-DD').

Hope it will help you.