0

I am trying to create an sObject from a Json string but get the error:

System.SObjectException: Illegal assignment from String to Date

This happens in line: newSobject.put(sObjectFieldsMap.get(key), value);

I do a value = Date.valueOf, why is this happening?

Apex

  public static sObject mapJsonfielsToSobject(String sObjectApiName, Map<String,Object> fieldMap, Map<String,String> sObjectFieldsMap){    
        system.debug('[TestApi]sObjectApiName: ' + sObjectApiName + ' ,fieldMap ' + fieldMap + ' ,sObjectFieldsMap: ' + sObjectFieldsMap);
        sObject newSobject = (sObject)Type.forName('Schema',sObjectApiName).newInstance();
        Map<String, Schema.sObjectField> targetFields = newSobject.getSObjectType().getDescribe().fields.getMap();
        system.debug('[TestApi]targetFields: ' + targetFields);
        for (String key : fieldMap.keySet()) {
            Object value = fieldMap.get(key);
            value = value == ''? null: value;
            switch on targetFields.get(key)?.getDescribe().getType() {
                when DATE {
                    value = Date.valueOf((String)value);
                }
                when DATETIME {
                    value = DateTime.valueOf((String)value);
                }
                when PERCENT, CURRENCY {
                    value = (Decimal)value;
                }
                when DOUBLE {
                    value = Double.valueOf(value);
                }
                when INTEGER {
                    value = (Integer)value;
                }
                when BASE64 {
                    value = Blob.valueOf((String)value);
                }
            }
            system.debug('[TestApi]targetFields: ' + targetFields);
            system.debug('[TestApi]key: ' + key + ' value: ' + value); 
         newSobject.put(sObjectFieldsMap.get(key), value);   
        } 
        return newSobject; 
    }
}

Json

{
      "Opp": [
        {
          "Oname": "Opp1",
          "CLdate":"2021/01/20",
          "Aname": "0012Y00001GYZDxAOL",
          "St":"Closed/Won"
        },
        {
          "Oname": "Opp2",
          "CLdate": "2021/01/20",
          "Aname": "0034Y00001GYZDxAOL",
          "St":"Closed/Won"
        }
      ]
}
2
  • 2
    I'm not your boss nor your client, and I don't know your use case, but if I were you, I'd be asking questions about this approach. It would be far more sensible to use the REST API directly instead of this apparently hackish approach, or at the least, try to implement a more reasonable JSON format to work with. – sfdcfox Feb 7 at 21:20
  • @sfdcfox Thanks!. This was a testcase for myself to comprehend mapping a dynamic json. My usecase is a daynamic api where an admin can map the json trough metadata where the JSON is not known upfront and flexibe to add fields to the mapping if the Json changes. I finally tested this with your answer and is working for a nested JSON 2 levels deep for now. – Thomas Feb 7 at 22:05
2

What happened is you tried to get CLDate from the targetFields map, which was null, so no conversion from String to Date occurred (you basically went down a default when else path, since the described type was null).

You need to change:

switch on targetFields.get(key)?.getDescribe().getType() {

To:

switch on targetFields.get(sObjectFieldsMap.get(key))?.getDescribe().getType() {

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.