2

I want to get dynamically all field for object (assume for Account) which are User reference . Using the describe method I can check that the field is Reference type but not clear how/if can I check the sobject type that is being referenced.

map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

for(Schema.SObjectField field : schemaMap.get('Account').getDescribe().fields.getMap().values()){
    Schema.DescribeFieldResult dr  = field.getDescribe();

    if(dr.getType() == DisplayType.Reference){
        System.debug(dr.getName());
    }
}

1 Answer 1

4

The getReferenceTo method provides the types (typically only one) so:

SObjectType deiredType = ...;

if (dr.getType() == DisplayType.Reference) {
    for (SObjectType t : dr.getReferenceTo()) {
        if (t == desiredType) {
            ...
        }
    }
}
1
  • Thanks. I looked at SF documentation. Don't know how I missed it...
    – Liron C
    May 13, 2018 at 15:03

You must log in to answer this question.

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