+ Start a Discussion

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();




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);


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());



//do something







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.