I'm trying to parse a Time field from my lightning component to my apex controller by using JSON and deserializing it in apex into an existing Object.
But unfortunately I'm always getting the error
Cannot deserialize instance of time from VALUE_STRING value 10:30:00.000Z
I already tried different Time formats and with/without quotes "" in my JSON
- 10:30
- 10:30:00
- 10:30:00.000
- 10:30:00.000Z
- T10:00:00.000Z
- 2018-06-07T10:00:00.000Z
But nothing works.
- My Org is on Spring 18 API Version 42
- I have an SObject (Event__c) with a Time Field
The problem is reproducable in anonymous apex with following code
Event__c workingObject = new Event__c();
workingObject.Name = 'Lorem';
workingObject.Public_Start_Time__c = Time.newInstance(10,10,10,10);
String serializedObject = JSON.serialize(workingObject);
String mixedObject = '{"Public_Start_Time__c": "' + workingObject.Public_Start_Time__c + '", "name": "Ipsum"}';
String manualObject = '{"Public_Start_Time__c": "2018-06-07T10:00:00.000Z", "name": "Dolor"}';
try {
Event__c banana = (Event__c)JSON.deserialize(serializedObject, Event__c.class);
}
catch(Exception ex) {
System.debug(ex.getMessage());
}
try {
Event__c banana = (Event__c)JSON.deserialize(mixedObject, Event__c.class);
}
catch(Exception ex) {
System.debug(ex.getMessage());
}
try {
Event__c banana = (Event__c)JSON.deserialize(manualObject, Event__c.class);
}
catch(Exception ex) {
System.debug(ex.getMessage());
}
which results in the 3 following messages
Cannot deserialize instance of time from VALUE_STRING value 10:10:10.010Z or request may be missing a required field
Cannot deserialize instance of time from VALUE_STRING value 10:10:10.010Z or request may be missing a required field
Cannot deserialize instance of time from VALUE_STRING value 2018-06-07T10:00:00.000Z or request may be missing a required field
Everything I found so far was for dates or datetimes but not time only.
Best Answer
This is probably a bug, and I'd recommend contacting Salesforce Support (If you haven't already).
Heres a repo I wrote using a custom object (
Test__c
, with a single time field,Some_Time__c
). It starts with the a demo usingTime
as the object, then runs the same code forTest__c
. The rawTime
object works, and has the same syntax as the JSON, but the JSON object fails with the same message.Some additional notes from my test runs:
Map<String, Object> record = (Map<String, Object>)JSON.deserializeUntyped(testString);
returns a validTime
string in the map.System.debug((Time)record.get('Some_Time__c'));
fails with a:Time.Parse
method, I cant figure out a good way to get the string value back into aTime
objectI spent some time writing an actual parser, which detects field types & behaves accordingly (using the fancy new
switch
statement). This should cover most use cases for people experiencing this issue until salesforce releases an actual fix.Theres a few other improvements I could make, such as using the
attributtes
node to create the type, instead of using it as a parameter, and some extra null handling/error handling, but hey, it works!As a bonus, it comes with a test class! I used a dummy object, with a ton of fields of just about every type, in order to ensure any field type works. Just about every
Schema.SOAPType
value is represented. Youll need to replace this with one of your own objects, but the idea is to have a generic parser, so it should work with any object & any number of fields.