super cool
super cool

Reputation: 6045

Unable to save Date to salesforce?

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"}

Upvotes: 1

Views: 1280

Answers (1)

Mike
Mike

Reputation: 3732

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

Upvotes: 1

Related Questions