0

Im trying to parse an ISO 8601 timestamp value consumed in a JSON data format to a DateTime field value in salesforce.

Class 1:

if ((fieldStatus == ‘normal’) || (fieldStatus == ‘overwritten’)) {
try {
value = VUtilities.formatSObjectFieldValue(flattenedFormData.get(key), ‘Account’,
field.Account_Field__c);
account.put(field.Account_Field__c, value);
} catch (Exception e) {
fieldStatus = ‘invalid’;
system.debug(LoggingLevel.ERROR,
‘#===[applyValuesToPatientRecords: ERROR! Invalid account field. ‘
+ ‘key = ‘ + key + ‘, ‘ + ‘value = ‘ + flattenedFormData.get(key)
+ ‘, field name = ‘ + field.Account_Field__c + ‘, exception = ‘ la e + ‘]===#’);
}
}

Vutilities has the below methods which the above class is calling:

public static Object formatSObjectFieldValue(Object fieldValue, String sObjectName, String fieldName) {
SObjectType objectType = Schema.getGlobalDescribe().get(sObjectName);
Integer stringMaxLength = objectType.getDescribe().fields.getMap().get(fieldName).getDescribe().getLength();
Schema.DisplayType fieldType = objectType.getDescribe().fields.getMap().get(fieldName).getDescribe().getType();

return formatFieldValue(fieldValue, fieldType, stringMaxLength); }

public static Object formatFieldValue(Object fieldValue, Schema.DisplayType fieldType, Integer stringMaxLength) {
Object returnValue = null;

if (fieldValue != null) {
if (
(fieldType == Schema.DisplayType.Combobox)
|| (fieldType == Schema.DisplayType.Address)
|| (fieldType == Schema.Displaytype.DateTime)
)

{
returnValue = stringifyObject(fieldValue);
if ((stringMaxLength != null) && (stringMaxLength > 0))
returnValue = ((String)returnValue).abbreviate(stringMaxLength);
}
/*else if (fieldType == Schema.DisplayType.DateTime) {
returnValue = DateTime.valueOf((String)fieldValue);
}
*/
else if (fieldType == Schema.DisplayType.DateTime) {
returnValue = ((DateTime)JSON.deserialize(JSON.serialize(fieldValue), DateTime.class));
}
}
}

return returnValue;
}

Im getting the below error

Error: exception = System.SObjectException: Illegal assignment from String to Datetime]=== Date sample - 2010-05-01T23:53:04.000+00:00

1

Looking at your code, you seem to be inspecting fieldType for DateTime twice:

if (
(fieldType == Schema.DisplayType.Combobox)
|| (fieldType == Schema.DisplayType.Address)
|| (fieldType == Schema.Displaytype.DateTime)  // inspect for DateTime here
)

{
returnValue = stringifyObject(fieldValue);
if ((stringMaxLength != null) && (stringMaxLength > 0))
returnValue = ((String)returnValue).abbreviate(stringMaxLength); // create a string
}
/*else if (fieldType == Schema.DisplayType.DateTime) {
returnValue = DateTime.valueOf((String)fieldValue);
}
*/
else if (fieldType == Schema.DisplayType.DateTime) { // inspect for datetime again 
 returnValue = ((DateTime)JSON.deserialize(JSON.serialize(fieldValue), DateTime.class));
}
}
}

The SobjectException is because you are trying to put an Object of type String into a Datetime field. So, the problem lies with why you are entering the first returnValue = ... statement - stringMaxLength != && stringMaxLength > 0 is where I'd look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.