I am querying all the fields on the Lead object and displaying them as a picklist on a visualforce page. I do not want the Lookup/Master-detail relationship fields to be displayed, though. For removing such fields, I have created a separate method in my class to remove such fields. I want to know is there a way of filtering such fields from Schema.getSObjectType().getDescribe().fields.getMap();

My code for querying all fields :

mapLeadFieldAPIToSchema = Lead.getSObjectType().getDescribe().fields.getMap();

2 Answers 2


The Lead.getSObjectType().getDescribe().fields.getMap(); line returns a Map with Schema.SObjectField objects as values.

The SObjectField object has a getDescribe() method returning a Schema.DescribeFieldResult object.

The Schema.DescribeFieldResult object contains a method getRelationshipOrder() which returns 1 if the field is a child of a master-detail relationship, 0 if the field is a parent of a master-detail relationship and null if not.

So in your case, the code will be:

Map<String, SObjectField> mapLeadFieldAPIToSchema = Lead.getSObjectType().getDescribe().fields.getMap();
for(String fieldName : mapLeadFieldAPIToSchema.keySet()){
    SObjectField field = mapLeadFieldAPIToSchema.get(fieldName);
    if(field.getDescribe().getRelationshipOrder() == 0){
         //the variable fieldName contains the name of a field which is the parent of a master-detail relationship

This is another way by which you can check for a field type! If it is a reference it will be lookup/Master-Detail. Schema.DisplayType fielddataType =fieldMap.get(fieldName).getDescribe().getType(); This will do the work.

List<SelectOption> leadList = new List<SelectOption>();
String objType='Lead';
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 Lead Object
    String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

    //get data types for each fields
    Schema.DisplayType fielddataType = 

    if(fielddataType != Schema.DisplayType.Reference) {
        leadList.add(new SelectOption(fieldLabel,fieldName));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .