function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nimbusprojectnimbusproject 

How do you Dynamically get Field types on an sObject type

Is there any way of getting the field types dynamically by just having a String containing the name of an sObject type? (such as Contact or Account)

 

How would you do the following dynamically rather than put in the object type statically (in this case Contact)

 

 

Contact.sObjectType.getDescribe().fields.getMap();

 So I pretty much need to do the following

 

<sObject type HERE>.getDescribe().fields.getMap();

 Only having a string of the object type name Contact.

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

You need to use the getGlobalDescribe() method to acquire the respective token:

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Schema.SObjectType ctype = gd.get('contact'); Map<String, Schema.SobjectField> fmap = ctype.getDescribe().fields.getMap();

 

 

 

All Answers

mtbclimbermtbclimber

You need to use the getGlobalDescribe() method to acquire the respective token:

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Schema.SObjectType ctype = gd.get('contact'); Map<String, Schema.SobjectField> fmap = ctype.getDescribe().fields.getMap();

 

 

 

This was selected as the best answer
melchisholm22melchisholm22
It works! Thanks!