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
Type Field Value | What the Field Object Contains |
---|---|
anytype | Any value of the following types: String, Picklist, Boolean, Integer, Double, Percent, ID, Date, DateTime, URL, orEmail. |
base64 | Base64-encoded arbitrary binary data (of type base64Binary) |
Boolean | Boolean (true or false) values |
Combobox | Comboboxes, which provide a set of enumerated values and allow the user to specify a value not in the list |
Currency | Currency values |
DataCategoryGroupReference | Reference to a data category group or a category unique name. |
Date | Date values |
DateTime | DateTime values |
Double | Double values |
Email addresses | |
EncryptedString | Encrypted string |
ID | Primary key field for an object |
Integer | Integer values |
MultiPicklist | Multi-select picklists, which provide a set of enumerated values from which multiple values can be selected |
Percent | Percent values |
Phone | Phone numbers. Values can include alphabetic characters. Client applications are responsible for phone number formatting. |
Picklist | Single-select picklists, which provide a set of enumerated values from which only one value can be selected |
Reference | Cross-references to a different object, analogous to a foreign key field |
String | String values |
TextArea | String values that are displayed as multiline text fields |
Time | Time values |
URL | URL values that are displayed as hyperlinks |