Let's say we have a Map<String,Object> we got from an lwc, and we need to create records for any given SObject that can change based on some custom metadata type records.
I have the following snippet but always get an error like invalid runtime conversion from type String to Schema.DisplayType
SObject record = (SObject) Type.forName(objectName)
.newInstance();
String fieldType = String.valueOf(record.getDescribe().fields
.getMap()
.get(fieldName)
.getDescribe()
.getType());
Type displayType = Type.forName(fieldType);
record.put(
objectField, //name of the field
(displayType) mapStringObjectWithMyValuesFromLWC.get(objectField)
);
I may be missing a better approach. All help and input is appreciated :(