Standard and Custom Objects in Salesforce have three-character prefixes that form the first part of every Record ID. For example, a User record with ID 00561000000Mjya has the prefix 005, which is the prefix for the User object.
For a complete list of Standard Field Record ID prefixes, see Standard Field Record ID Prefix Decoder.
In some scenarios, you may want to determine the name of the object associated with a given prefix programmatically using Apex code. This article provides two approaches: a simple one-line method and a full custom class for reusable lookup logic.
The simplest way to find the object type from a Record ID is using the getSObjectType() method on an Apex Id variable. Run the following snippet in Execute Anonymous Apex in the Developer Console. Replace 00561000000Mjya with the Record ID you want to look up. The debug log outputs the sObject API name (for example, User):
Id sampleid = '00561000000Mjya';
System.debug('Object is: ' + sampleid.getSObjectType());
If you need to identify object types programmatically in Apex code — for example, in a trigger, batch class, or LWC controller — use the SchemaGlobalDescribe class below. This approach iterates through all sObject types in the org and matches the 3-character key prefix extracted from the Record ID.
Important: In orgs with a large number of custom objects, this approach may approach the Apex CPU time limit. To reduce the object list, filter out managed package objects by checking !sObj.contains('__') inside the loop.
The following class provides the findObjectNameFromRecordIdPrefix method that accepts either a full Record ID or just the 3-character prefix:
public class SchemaGlobalDescribe {
public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix) {
String objectName = '';
try {
// Extract the 3-character prefix from the ID
String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0, 3);
// Get all sObject types in the org
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
// Loop through all sObject types and match the prefix
for (Schema.SObjectType stype : gd.values()) {
Schema.DescribeSObjectResult r = stype.getDescribe();
String prefix = r.getKeyPrefix();
if (prefix != null && prefix.equals(myIdPrefix)) {
objectName = r.getName();
break;
}
}
} catch (Exception e) {
System.debug(e);
}
return objectName;
}
}
To use this class, run the following in Execute Anonymous Apex:
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.debug(objectName); // Outputs: Case
The following test class validates the findObjectNameFromRecordIdPrefix method across four scenarios: positive match, negative assertion, null prefix, and exception handling:
@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, '');
}
}
getSObjectType() approach (Approach 1) is the preferred method for simple, one-time lookups and has minimal performance impact.SchemaGlobalDescribe class (Approach 2) is suitable for programmatic lookups in Apex code but should be used with caution in orgs with many objects due to potential CPU limit concerns.Schema.getGlobalDescribe() loop, add a condition such as if (!sObj.contains('__')) before processing each sObject type.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.