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



3 comments:

Unknown said...

Thanks for sharing this pretty article, i am getting some useful information regarding salesforce, keep your updates regularly...
Regards,
Salesforce Training in Chennai|Salesforce Training Institute in Chennai

Unknown said...

I used your logic to create a useful method that returns all fields of the data type and SObject you specify.

public static List getSObjectFieldsByType(String objType, String dataType) {
List resultFieldList = new List();

// Get a map of all sObject names (keys) to sObject tokens (values) for
// the standard and custom objects defined in the org
Map schemaMap = Schema.getGlobalDescribe();

// Get the sObject type specified by the method parameter objType from the schema map
// TODO what happens if the objtype doesn't exist in the schemaMap
Schema.SObjectType objSchema = schemaMap.get(objType);

// Get a map of the fieldNames (keys) to SObjectFields (values) for the object
// specified by the method parameter objType
Map fieldMap = objSchema.getDescribe().fields.getMap();

for (String fieldName : fieldMap.keySet()) {
// get data types for each field
Schema.DisplayType fieldDataType = fieldMap.get(fieldName).getDescribe().getType();

// get the fields that match the datatype that is passed into the method and add them to a list.
// fieldDataType is an enum. Therefore I use the enum method name() to return
// the name of the Enum item as a string in order to compare the passed in parameter
if (fieldDataType.name().equalsIgnoreCase(datatype)) {
resultFieldList.add(fieldMap.get(fieldName));
}
}

return resultFieldList;
}

You can easily have this return fields of multiple data types by changing the parameter String datatype to Set datatypes. Then change the final if statements to if(datatypes.contains(fieldDataType.name()) { ... }

Unknown said...

How to check for Number fields and checkbox type fields, it doesn't allow on integer to check number field.

Post a Comment

 
| ,