I have encountered the error System.SObjectException: Illegal assignment from String to Datetime and I can't find what is the reason for this error.
The context is as follows. I have a class with the @RestResource
annotation and part of its logic is shown below:
I use the following code snippet to maintain a relationship between the fields sent in the JSON and the field it references in Salesforce.
private static Map<String, Schema.SObjectField> notNullabelFields = new Map < String, Schema.SObjectField> {
'origin' => Account.Origin__c,
'webId' => Account.WebId__c,
'documentId' => Account.DocumentId__c,
'phone' => Account.Phone,
'language' => Account.Language__c,
'email' => Account.PersonEmail,
'prefContactStart' => Account.PreferedContactStart__c,
};
I maintain a wrapper class where I deserialize the JSON request I receive.
private class RequestData
{
public String origin {
get;
set {origin = value; setReceivedFields('origin');}}
public String webId {
get;
set {webId = value; setReceivedFields('webId');}}
public String documentID {
get;
set {documentId = value; setReceivedFields('documentID');}}
public String phone {
get;
set {phone = value; setReceivedFields('phone');}}
public String language {
get;
set {language = value; setReceivedFields('language');}}
public String email {
get;
set {email = value; setReceivedFields('email');}}
public Datetime prefContactStart {
get;
set {prefContactStart = value; setReceivedFields('prefContactStart');}}
private Object get(String property)
{
String jsonFormatRequestDataInstance = Json.serialize(this);
Map<String, Object> untypedRequestDataInstance = (Map<String, Object>) JSON.deserializeUntyped(jsonFormatRequestDataInstance);
return untypedRequestDataInstance.get(property);
}
}
The get
method of the wrapper class RequestData
is used to dynamically obtain the value of the property.
When the WS is called, the request is deserialized into an object of the wrapper class type.
RequestData reqData = (RequestData) JSON.deserializeStrict(request.requestBody.toString(), RequestData.class);
The problem is happening in the code snippet I attach below. What I am trying to do is to dynamically map the values received in the JSON into the appropriate fields in a Salesforce record, as long as these values are not null.
Account acc = new Account();
for (String field : notNullabelFields.keySet())
{
if (reqData.get(field) != null)
{
acc.put(notNullabelFields.get(field), reqData.get(field));
}
}
When I send the request from Postman the following error appears:
System.SObjectException: Illegal assignment from String to Datetime
I don't understand the reason.
- Deserialization in the wrapper class is correctly performed (checked)
- The
get
method of the wrapper classRequestData
returns a value of typeObject
. - The second parameter expected by the
put
method of theSObject
class is of typeObject
.
At what point is the conversion from Object
to String
done?
I have tested 'manually' mapping the value of the JSON field prefContactStart
to the Salesforce field Account.PreferredContactStart__c
and no exception is thrown. In fact, the field in Salesforce is updated correctly.
acc.PreferedContactStart__c = reqData.prefContactStart;
Edit:
Attached is an example of the JSON I receive;
{
"origin":"origin example",
"webId":"1234",
"documentId": "1111111",
"phone":"+34666666666",
"language": "German",
"email": "[email protected]",
"prefContactStart": "2023-06-26T11:00:00.000Z"
}
prefContactStart
I receive something like that2023-06-26T11:00:00.000Z
. I will update my initial message to attach an example of the JSON I am receiving.