If you know the field you want to examine at compile-time, then you can get the describe information for that field directly.
Opportunity.AccountId.getDescribe().getReferenceTo()[0].getDescribe().getName()
If this is more dynamic, and you don't know which field to examine until run-time, then there's no way around needing to get the field map from the DescribeSObjectResult
, though since you can get a Map<String, DescribeFieldResult>
(the keys are API names) you can simply get the specific field you're looking for.
Opportunity.SObjectType.getDescribe().fields.getMap().get('AccountId').getDescribe().getReferenceTo()[0].getDescribe().getName())
If you have an instance of an SObject with the target relationship field populated, you can use that Id to get at the SObject type
myOpp.AccountId.getSObjectType().getDescribe().getName()
Id
prefix is not relevant for the question, as there's noId
to work with at all.Account.Id
is a plainString
. My question is, knowing where the relation starts at (e.g. an Opportunity), is it possible to dynamically determine what kind of anSObject
"Account" substring might refer to. I don't have an Opportunity which has Account and Id selected, nor am I interested in it. I've edited the question to make it clearer.