getSobjectType() can be used to get Object Name from the Id value using Salesforce Apex.
So, we can get the object name using record id. Check the following code for reference.
Sample Code:
Id recId = '0013t00002XXRyDAAX';
Schema.SObjectType objType = recId.getSobjectType();
System.debug(
'Object Type is ' +
objType
);
Visualforce page:
<apex:page
controller="sample"
sidebar="false" >
<apex:form >
<apex:pageblock id="pg" >
<apex:pageblockSection >
<apex:pageBlockSectionItem>
Record Id
</apex:pageBlockSectionItem>
<apex:pageblockSectionItem >
<apex:inputtext value="{!recId}"/>
</apex:pageblockSectionItem>
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton
value="Find"
action="{!find}"
reRender="pg"/>
</apex:pageBlockButtons>
<apex:outputText >
The object type is : {!objType}
</apex:outputText>
</apex:pageblock>
</apex:form>
</apex:page>
Apex: Controller:
public with sharing class sample {
public Id recId { get; set; }
public Schema.SObjectType objType { get; set; }
public void find() {
objType = recId.getSobjectType();
System.debug(
'Object Type is ' +
objType
);
}
}
Output: