Monday, June 23, 2014

How to get field data types for each fields in Salesforce objects?

How to get field data types for each fields in Salesforce objects?


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_describe.htm#apex_displaytype



2 comments:

Post a Comment

 
| ,