The getType
method you are calling directly returns a DisplayType
value rather than a String
so change your code to:
DisplayType displayType = record.getDescribe().fields
.getMap()
.get(fieldName)
.getDescribe()
.getType();
See the DescribeFieldResult documentation for more detail.
PS
In response to the first comment, in your approach, you are running Type.forName
on one of the enum
values e.g. DisplayType.Picklist
not the enum
type DisplayType
. So it's not going to work even if Apex treated enum
as a special kind of class
.
Also note that trying to cast a field value to a DisplayType
won't work. For types other than strings you will likely need to write your own type conversion code based on the DisplayType
or likely better based on the SOAPType
that is also available from a describe call.
Given this complexity do consider if your overall approach here is appropriate.