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
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com 

how to get object name from id value in salesforce

Is there any method through which i can get the lookup field object name through id only that comes in that lookup field??

 

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Or, in one statement using ID methods

 

String sObjName = myId.getSObjectType().getDescribe().getName();

 

All Answers

Puja_mfsiPuja_mfsi

Hi,

You can easily get the object Name from Record Id:

 

 public string findObjectAPIName( String recordId ){
        if(recordId == null)
            return null;
        String objectAPIName = '';
        keyPrefix = recordId.substring(0,3);
         for( Schema.SObjectType obj : Schema.getGlobalDescribe().Values() ){
              String prefix = obj.getDescribe().getKeyPrefix();
               if(prefix == keyPrefix){
                         objectAPIName = obj.getDescribe().getName();
                          break;
                }
         }

         return objectAPIName;

}

 

 

 

Please let me know if u have any problem on same and if this post helps you give KUDOS by click on star at left.

crop1645crop1645

Or, in one statement using ID methods

 

String sObjName = myId.getSObjectType().getDescribe().getName();

 

This was selected as the best answer
Kasireddy VKasireddy V
Helpful
Umesh Pal 18Umesh Pal 18
To find the sObject name form recordId, use the below line of code and your variable data type should be Id not string in your apex method
Id recordId  = '005xxxxxxxxxx';
String sObjName = recordId.getSObjectType().getDescribe().getName();