You need to sign in to do that
Don't have an account?

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.
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
You can access the field type in the sae way that you access the label - just use the getType method. E.g.
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..
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
}