html code

Determine Object Type Based on Record ID Prefix

Summary

Standard and Custom Objects in Salesforce have three character prefixes which form the first part of the Record ID. 
For example, a User record with ID 00561000000Mjya has the prefix 005, which is the prefix for the User Object. 

For a list of Standard Field Record ID prefixes, please see Standard Field Record ID Prefix Decoder.

In some scenarios, you may want to find out the name of the Object associated with the prefix using Apex code. 

Solution

If the Object id is ‘00561000000Mjya’, run the following code in ‘Execute Anonymous Apex’

Id sampleid = '00561000000Mjya'; System.debug('object is '+ sampleid.getsobjecttype());

You can use an alternate approach. Create the following Class SchemaGlobalDescribe. 
Please note, if the org has many objects you may run into Apex CPU limit error. In that case, please adjust the following code to exclude managed packages or filter accordingly. 

public class SchemaGlobalDescribe{
    public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix){
        String objectName = '';
        try{
            //Get prefix from record ID
            //This assumes that you have passed at least 3 characters
            String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3);
             
            //Get schema information
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
             
            //Loop through all the sObject types returned by Schema
            for(Schema.SObjectType stype : gd.values()){

                //if (!sObj.contains('__')) to exclude managed package objects

                Schema.DescribeSObjectResult r = stype.getDescribe();
                String prefix = r.getKeyPrefix();
                System.debug('Prefix is ' + prefix);
                 
                //Check if the prefix matches with requested prefix
                if(prefix!=null && prefix.equals(myIdPrefix)){
                    objectName = r.getName();
                    System.debug('Object Name! ' + objectName);
                    break;
                }
            }
        }catch(Exception e){
            System.debug(e);
        }
        return objectName;
    }
}

Run the provided code snippet in the Developer Console to retrieve the Object name associated with the Record ID prefix:

Run the provided code snippet in the Developer Console to retrieve the Object name associated with the Record ID prefix:




You can employ this class along with the subsequent test class to generate a custom Visualforce page or utilize it in a Lightning component.

@isTest
private class SchemaGlobalDescribeTests{
    @istest
    private static void testMethodPositive(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
        System.assertEquals(objectName,'Case');
    }
    @isTest
    private static void testMethodNegative(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
        System.assertNotEquals(objectName,'Account');
    }
    @isTest
    private static void testMethodNull(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('101');
        System.assertEquals(objectName,'');
    }
    @isTest
    private static void testMethodException(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('10');
        System.assertEquals(objectName,'');
    }
}