Hello Everyone, In today's post we will going to see an example that helps us to find sobject name/api name that is relatable field using lookup and master-detail relationships on any object. This code will play a vital role to do dynamic apex coding. so let's begin,
Sobject Name/API name of any Lookup or MDR field
Use below code to achieve the requirement
Code Syntax:
String sObjectName='Contact'; // replace with your sobjectOutput:Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
String fieldName='AccountId'; // replace with your reference field
Schema.DescribeFieldResult f = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().get(fieldName).getDescribe();
for(Schema.SObjectType reference : f.getReferenceTo()) {
System.debug('Reference object name: ' + reference.getDescribe().getName());
System.debug('Reference object label: ' + reference.getDescribe().getLabel());
}
Thank you.
0 Comments
Post a Comment