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
Priyanka ChandrakanthPriyanka Chandrakanth 

Determine Custom object from its object ID

Can anyone please tell me how can I determine the API name of custom object from its ID?
Balaji BondarBalaji Bondar
Hi Priyanka,

Use below code :
String myIdPrefix = String.valueOf(someId).substring(0,3); //get just the prefix
Map<String, Schema.SObjectType> gd = 
Schema.getGlobalDescribe(); 
for(Schema.SObjectType stype : gd.values())
{
    Schema.DescribeSObjectResult r = stype.getDescribe();
    String prefix = r.getKeyPrefix();
    System.debug('Prefix is ' + prefix);
    if(prefix!=null && prefix.equals(myIdPrefix))
    {
        System.debug('Stop hammer time! ' + r.getName());
        break;
    }
}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
NekosanNekosan
Did you try this Schema.Sobjecttype objtype=id.getSObjectType(); ? 
Priyanka ChandrakanthPriyanka Chandrakanth
That was interview question, for Standard objects, I could get from first three 3 characters of ID, but didn't know how to get it for custom objects.