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.
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;
}
}
Execute the following snippet of code in the Developer Console to find the Object name based on the Record ID prefix:
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.debug(objectName);
You could use this class, as well as the following test class, to create a custom VisualForce page or use with 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,'');
}
}
Id Class - Example: Getting an sObject Token from an ID using getSObjectType
000388019

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.