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
MFSDevMFSDev 

How to get data Type of Object field

Hi,

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.

If there is way,please help.

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
MFSDevMFSDev

Thanks,I have got it!

 

        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

 

         }

All Answers

bob_buzzardbob_buzzard

You can access the field type in the sae way that you access the label - just use the getType method.  E.g.

 

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

 

MFSDevMFSDev

Thanks a lot for your reply.
Actually what is need is that if an object's field is text area then it will not be added at a Picklist. So How can I check its value with TextArea.

I have seen in dubug log that its printing textArea as TextArea..but how can I give if condition..

 

 

 

 

 

 

MFSDevMFSDev

Thanks,I have got it!

 

        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

 

         }

This was selected as the best answer