+ Start a Discussion
TarentTarent 

How to get data Type of Object field

I need to check datatype for some fields of my custom object.

String type= 'MyCustomObject__c'; // Say,this is my object
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(type);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
for (String fieldName: fieldMap.keySet()) {
String mylabel;
//It provides to get the object fields label.
mylabel = fieldMap.get(fieldName).getDescribe().getLabel();
}
Now this type i.e. Object Name is varying time to time and I need to check fields data type.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

String type='MyCustObject_c';

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

        Schema.SObjectType leadSchema = schemaMap.get(type);

        Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

     

        for (String fieldName: fieldMap.keySet()) {

 

        //It provides to get the object fields label.

        String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

 

       //It provides to get the object fields data type.

        Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();

         if(fielddataType != Schema.DisplayType.TextArea)

               //do something

         if(fielddataType != Schema.DisplayType.String)

               //do something

 

         }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

String type='MyCustObject_c';

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

        Schema.SObjectType leadSchema = schemaMap.get(type);

        Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

     

        for (String fieldName: fieldMap.keySet()) {

 

        //It provides to get the object fields label.

        String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

 

       //It provides to get the object fields data type.

        Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();

         if(fielddataType != Schema.DisplayType.TextArea)

               //do something

         if(fielddataType != Schema.DisplayType.String)

               //do something

 

         }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Shahroz BegShahroz Beg
I want to convert each field from fieldMap to the corresponding data type.
Sandeep LohaniSandeep Lohani

I was looking for something like this but was also interested in api name of the fields for that custom object. To add to Navatar_DbSup response
If you would do this in the above code sample  after this line 

//It provides to get the object fields label.
        String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

//It provides api name of the field
        String fieldDataType = fieldMap.get(fieldName); 

Admin User 9731Admin User 9731
Hi, I am new to SFDC.

Please bear with me, where do I run this query to get the Field Name of the respective schema?