1

I have to get the object name of the lookup field in any object. I am using:

sobjectType objectType=Schema.getGlobalDescribe().get('SA_S3_File__c').

then, to get the lookup field name, i am using

"fieldsMap.get(field).getDescribe().getType()+''=='REFERENCE'"

after getting the fieldname of lookup, how can i get the object name of those lookup fields?

2 Answers 2

2

I think you can do something like this:

Schema.DescribeFieldResult f = fieldsMap.get(field).getDescribe();   
System.debug(f.getRelationshipName())    
for(Schema.SObjectType reference : f.getReferenceTo())
{
    System.debug(reference.getDescribe().getName());
}

See docs here for more info.

2

Same as Phil Hawthorn's answer but with a loop and you can just "toString" an SObjectType to get its name, no need for another describe:

SObjectType t = ...;

DescribeSObjectResult dt = t.getDescribe();
for (SObjectField f : dt.fields.getMap().values()) {
    DescribeFieldResult df = f.getDescribe();
    if (df.getType() == DisplayType.Reference) {
        for (SObjectType r : df.getReferenceTo()) {
            System.debug('field ' + df.getName() + ' references type ' + r);
        }
    }
}

Normally the getReferenceTo only returns one type.

You must log in to answer this question.

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