Identify Object Type from Record ID

Ever wondered how to figure out what type of object a record ID belongs to in Salesforce?

You might see an ID like 0015g00000XYZ123 and ask yourself:

“Hmm… is this an Account? A Contact? Maybe a custom object?”

In Salesforce, every object has a unique ID prefix and that prefix tells you what type of object it is. In this post, we’ll write an Apex method that accepts a record ID and returns the object API name using the powerful Schema class.

If you’re learning Apex, this use case is a fantastic way to understand:

  • How Salesforce IDs work

  • What Schema.getGlobalDescribe() does

  • How to dynamically explore your org’s object model

🧠 What Are We Trying to Do?

 

We want a method that:

  • Accepts any Salesforce record ID (like 001..., 003..., etc.)

  • Checks its first 3 characters (called the key prefix)

  • Uses Salesforce’s schema describe methods to match that prefix with the correct object

  • Returns the object name (like Account, Contact, etc.)

This can be useful for:

  • Debugging

  • Building tools that work across multiple objects

  • Logging or generic automation features

🔍 Code Explanation

 
public with sharing class ApexUseCaseNineteen {
    
    public static String checkObjectType(Id recordId){
        String objectType = '';

We define a class called ApexUseCaseNineteen and a method checkObjectType that takes a single parameter: a record ID. We’ll use this to determine the object type.

        Map<String, Schema.SObjectType> globalDescMap = Schema.getGlobalDescribe();

This is the key part!
Schema.getGlobalDescribe() returns a map of all objects in your org, where the keys are object names (Account, Contact, Opportunity, etc.), and the values are the describe info.

        for(String eachString : globalDescMap.keySet()){
            String keyPrefix = globalDescMap.get(eachString).getDescribe().getKeyPrefix();

We loop through each object and grab its key prefix (the first 3 characters that all records of that object start with).

            if(keyPrefix != null){
                if(String.valueOf(recordId).startsWith(keyPrefix)){
                    objectType = eachString;
                }
            }

If the current object’s prefix matches the start of the given record ID, we’ve found our object type!

        return objectType;
    }
}

Finally, we return the object name. If no match is found, we return an empty string.

✅ Why This Is Useful

 

This method is powerful in dynamic or generic code where you:

  • Don’t know the object type ahead of time

  • Are dealing with multiple object types (like in LWC or triggers)

  • Want to write tools for auditing or analyzing data

You can also enhance this method to:

  • Return the label (instead of the API name)

  • Work with 15 or 18-character IDs

  • Throw an error if no match is found

🧾 Final Code Snippet

 
public with sharing class ApexUseCaseNineteen {
    public static String checkObjectType(Id recordId){
        String objectType = '';
        
        Map<String, Schema.SObjectType> globalDescMap = Schema.getGlobalDescribe();
        for(String eachString : globalDescMap.keySet()){
            
            String keyPrefix = globalDescMap.get(eachString).getDescribe().getKeyPrefix(); 
            if(keyPrefix != null){
                if(String.valueOf(recordId).startsWith(keyPrefix)){
                    objectType = eachString;
                }
            }
        }
        return objectType;
    }
}

🎥 Watch It in Action!

 

Want to see this in action? I created a short video showing how to pass in a record ID and watch it return the correct object type from your org.

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)