Loading

Find Object type from Record ID prefix

Data pubblicazione: Jun 3, 2026
Descrizione

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.

Risoluzione

Approach 1: Using getSObjectType() Method (Simple)

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());

Approach 2: Using Schema.getGlobalDescribe() (Custom Class)

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

Apex Test Class

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, '');
}
}

Important Considerations

  • The getSObjectType() approach (Approach 1) is the preferred method for simple, one-time lookups and has minimal performance impact.
  • The 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.
  • To exclude managed package objects from the Schema.getGlobalDescribe() loop, add a condition such as if (!sObj.contains('__')) before processing each sObject type.
Numero articolo Knowledge

000388019

 
Caricamento
Salesforce Help | Article