2

how to i get datatype of any filed in SOQL?

1 Answer 1

5

we can get the all the standard and custom objects fields data types using the getGlobalDescribe, getDescribe, getType.

String objType=’Account’;
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(objType);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

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

    //get all the fields label for Account Object
    String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

    //get data types for each fields
    Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
    if(fielddataType == Schema.DisplayType.TextArea) {
        /* build your logic if the Field data type is TextArea */
    }

    if(fielddataType == Schema.DisplayType.String) {
        /* build your logic if the Field data type is String */
    }

    if(fielddataType == Schema.DisplayType.Integer) {
        /* build your logic if the Field data type is Integer */
    }

    if(fielddataType == Schema.DisplayType.DateTime) {
        /* build your logic if the Field data type is DateTime */
    }

}

Here Schema.DisplayType enum value is returned by the field describe result’s getType method.

Click here for more details:

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_fields_

2
  • 1
    It should be == instead of !=
    – BarCotter
    Jul 20, 2015 at 10:32
  • ohhh yaa thanx!!! will update Jul 20, 2015 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .