You need to sign in to do that
Don't have an account?
How to check the field data type of the object in apex
Actually i want to check whether if a field is of lookup type and do something if yes.
i tried the following
String ltype='Lead';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(ltype);
Map<String, Schema.SObjectField> lfieldMap = leadSchema.getDescribe().fields.getMap();
for (String fieldName: lfieldMap.keySet()) {
//It provides to get the object fields label.
String lfieldLabel = lfieldMap.get(fieldName).getDescribe().getLabel();
//It provides to get the object fields data type.
Schema.DisplayType lfielddataType = lfieldMap.get(fieldName).getDescribe().getType();
if(lfielddataType==Schema.DisplayType.ID)
system.debug(':::::::::::lfieldType'+lfielddataType);
system.debug(':::::::::::::::+lfieldLabel'+lfieldLabel);
}
but is displaying all types
any suggestions
Hi Raj,
you can get object field type by using the following code.......
Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();
for(String s : objectFields.keySet())
{
Schema.DescribeFieldResult lfieldLabel = objectFields.get(s).getDescribe();
Schema.DisplayType dType = lfieldLabel.getType();
string fieldType = String.ValueOf(dType);
system.debug('******'+fieldType);
}
string s='Lead';
Map < String, Schema.SObjectType > m = Schema.getGlobalDescribe();
Schema.SObjectType sT = m.get(s);
Schema.DescribeSObjectResult r = sT.getDescribe();
Map<String, Schema.SObjectField> mapData = r.fields.getMap();
for(Schema.SObjectField sField:mapData.values()){
F= sField.getDescribe();
//getType will return the field Data Type like Double, Phone etc
string sdataType= string.valueOf(F.getType());
if(sdataType=='REFERENCE'){
//do something
}else{
//nothing
}
}
you need to check condition 'REFERENCE'. see the above smaple. If you want to have check and condition for custom lookup then you need to add more condition as string sBoolean=string.valueOf( F.isCustom()); // will return True/False.
Hope this will help.