Whats the frequency of the letter v in:
Q: System.SObjectException: Illegal assignment from String to Date 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
        }
      ]
}


A: 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() {


user
There are 27 "v"s in the text you provided.
assistant