Saturday, July 13, 2019

How to get object name from id value in salesforce

Hi with the help of below code, we can find objec name by salesforce RecordID

(1)
Id someId = '00590000000eF8z';
System.debug(someId.getSObjectType());

(2)
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;

}