You need to sign in to do that
Don't have an account?
How to get data Type of Object field
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.
Hi,
Try the below code snippet as reference:
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
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Hi,
Try the below code snippet as reference:
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
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
I was looking for something like this but was also interested in api name of the fields for that custom object. To add to Navatar_DbSup response
If you would do this in the above code sample after this line
//It provides to get the object fields label.
String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
//It provides api name of the field
String fieldDataType = fieldMap.get(fieldName);
Please bear with me, where do I run this query to get the Field Name of the respective schema?