+ Start a Discussion
Linga_RaminLinga_Ramin 

How do I fetch all the Lookup fields of an object using Schema.DisplayType?

I have a requirement that I have to fetch all the Account Lookup fields.I'm using Schema.DisplayType for fetching Text,Picklistlist fields.But for lookup fields I don't know how to fetch.Can anyone help me?
Best Answer chosen by Linga_Ramin
Abhishek BansalAbhishek Bansal
Hi,

For lookup fields please use the Reference keyword. Below is the sample code for your help:
String objType = 'Account';
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 Account Object
    String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

    //get data types for each fields
    Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
    if(fielddataType == Schema.DisplayType.Reference) {
        system.debug('Field Label===='+fieldLabel);
    }
}

Please let me know if you need any further help on this.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi,

For lookup fields please use the Reference keyword. Below is the sample code for your help:
String objType = 'Account';
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 Account Object
    String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

    //get data types for each fields
    Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
    if(fielddataType == Schema.DisplayType.Reference) {
        system.debug('Field Label===='+fieldLabel);
    }
}

Please let me know if you need any further help on this.

Thanks,
Abhishek Bansal.

This was selected as the best answer
Linga_RaminLinga_Ramin
Thanks Abhishek 
Deepali KulshresthaDeepali Kulshrestha
Hi,

Try this
-I have done this in my org and it runs fine for me.

SObjectType objToken = Schema.getGlobalDescribe().get('Account');
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fieldsObj = objDef.fields.getMap();
Set<String> fieldSet = fieldsObj.keySet();
DescribeFieldResult selectedField;
for(String field : fieldSet){
    SObjectField fieldToken = fieldsObj.get(field);
    selectedField = fieldToken.getDescribe();
    if(selectedField.getType() == Schema.DisplayType.REFERENCE){
        system.debug('field :: '+field);
    }
}

if you need to describe all fields and wonder which ones are lookups try this:-

for(Schema.ChildRelationship cr : Account.SObjectType.getDescribe().getChildRelationships()){
    System.debug(cr.getChildSObject() + '.' + cr.getField() + ' reversed is: ' + cr.getRelationshipName());
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha