1
votes

To my surprise , its not st.forward thing to do; saving Date to Salesforce .

I'm trying to update one field which is of type Date but it throws me some weird error .

Code :

var objSer = new JavaScriptSerializer();
string json = objSer .Serialize(new{
          startdate = sfdcValue
       });
MyUpdateMethod("objectName/" + id, json);

I tried to convert date to IS0 8601 standard (as suggested over SO)

1.) DateTime.UtcNow.ToString("s",System.Globalization.CultureInfo.InvariantCulture)

2.) DateTime.UtcNow.ToString("o")

Error Info :

{"message":"Cannot deserialize instance of double from VALUE_STRING value 2017-05-26T10:31:40.5790708Z or request may be missing a required field at [line:1, column:2]","errorCode":"JSON_PARSER_ERROR"}

1
Do you just want the date or the datetime? I have had no problems getting SF to accept date values as strings in the form "YYYY-MM-DD". Maybe try splitting the datetime you are creating into just the date portion. SF does have some weirdness that if you give it a bad date like "2017-03-35" it will show up as 04/04/2017 on the backend. - Rob Davis
@RobDavis i have tried date (YYYY-MM-DD) earlier but it throws me the same error . interestingly , i can update date on portal for the same field tough . - super cool
It also seems weird that the parse error says it can't deserialize to a double rather than a date or datetime. I know this is a stupid question, and given your rep, I feel even dumber for asking it, but are you sure a) the field is actually a Date in SF b) that you have assigned the correct field name to the variable startdate? - Rob Davis
yeah error says 'double' but even for someone sake i expected it to be date/time something relevant . i do verified a) b) you referring to but as mentioned it too weird @RobDavis . - super cool

1 Answers

1
votes

You didn't elaborate on which method you are using to communicate between the server and client. I am using Javascript Remoting (@RemoteAction on the apex method) and I ran into this issue. For me, the date and datetime fields were being expected by Salesforce as date serials (your mileage may vary if using a different access method).

Given I was dealing with a dynamic list of fields, I ended up with a pair of marshall / unmarshall functions on the client that created client-only slave fields I removed before sending the data back to Salesforce (NB: the example below is javascript not c#). In your case, a marshall / unmarshall may take a different approach:

// Provide an client-only date field based on a date serial (SFDC input)
function createDateDerivedField(currentRecord, fieldName) {
    Object.defineProperty(currentRecord, fieldName + '__ui', {
        enumerable: true,
        get: function () {
            return currentRecord[fieldName] == null ? null : new Date(currentRecord[fieldName]);
        },
        set: function(newValue) {
            // Update the original field
            currentRecord[fieldName] = newValue == null ? null : (new Date(newValue)).getTime(); // Convert back to date serial
        }
    });
}