function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vinayakamurthy15vinayakamurthy15 

getting api name

how do i can get api name from relationship name of an object?

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Your requirement is not very clear. But, here i would like to tell you that, You can get your realted object's API name through appending __r with the lookup field you have.

 

For example,

 

From Object1__c you have to query the Object2__c's field name 'Field1__c' then,

 

Object1__c.Object2__r.Field1__c

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

vinayakamurthy15vinayakamurthy15

I have a ui where i pass a relationship name of an object, i some how want to get the api name of that object whose relationship name i have passed....

Dhaval PanchalDhaval Panchal

You want api name of parent object of lookup/masterdetail field?

 

Use below function.

 

    Public Static String getParentObjectNameByReferenceFieldName(String ObjectName, String RefField){
        String ParentName;
        Map<String, Schema.SObjectField> objFieldMap = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
        Schema.SObjectField field = objFieldMap.get(RefField);
        if(String.valueOf(field.getDescribe().getType()) == 'reference'){
            Schema.SObjectType obj = field.getDescribe().getReferenceTo()[0];
            ParentName = String.valueOf(obj);
        }
        else{
            ParentName = ObjectName;
        }
        return ParentName;
    }

 This function will return parent object api name.

for example if you call this function like below

clsGeneral.getParentObjectNameByReferenceFieldName('Contact', 'AccountId');

This function will return "Account".

 

You want same?