+ Start a Discussion

getting api name

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

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan



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,




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.


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);
            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?