Salesforce Quick Tips

Salesforce Quick Tip – How to Get The SObject Type of a Record Id in Apex

2 min read

Subscribe on YouTube

Why This Is Useful

Sometimes all you’ve got is a record Id. Not the record, just the eighteen character Id. And you need to know what object it belongs to.

This comes up constantly when you’re building genuinely dynamic Apex, the sort of class that can work against any object in your org rather than being hardcoded to Account. It’s a good tool to have in your pocket for writing flexible, reusable code.

And it’s one line.


The Code

Apex
public class RecordIdMagic
{
    public String getObjectType(Id recordId)
    {
        return recordId.getSObjectType().getDescribe().getName();
    }
}

Let’s break that chain down, because there are three useful things happening:

recordId.getSObjectType() works out which SObject that Id represents. Salesforce encodes the object into the first three characters of every record Id, so this doesn’t cost you a query.

.getDescribe() gets you the full describe result for that object. There’s a lot in there beyond the name.

.getName() pulls out the API name.

Test it from anonymous Apex:

Apex
List<Account> accts = [SELECT Id FROM Account LIMIT 1];
System.debug(new RecordIdMagic().getObjectType(accts[0].Id));   //Account

There’s More In There Than The Name

getDescribe() returns a genuinely useful pile of information, and it’s worth a poke around:

Apex
Schema.DescribeSObjectResult describe = recordId.getSObjectType().getDescribe();

describe.getName();          //API name, e.g. 'Account'
describe.getLabel();         //what users see, e.g. 'Account'
describe.isCustom();         //is it a custom object?
describe.isAccessible();     //can the running user read it?
describe.isCreateable();     //can they create one?
describe.isDeletable();      //can they delete one?
describe.getKeyPrefix();     //the 3 character prefix, e.g. '001'

Those permission checks are more than a curiosity. If you’re writing dynamic Apex that queries or updates whatever object it’s handed, checking isAccessible() and friends is how you respect the running user’s permissions properly. Static analysis tools like the SFDX Scanner will flag you if you don’t.


A Practical Use

Where this really earns its keep is dynamic queries:

Apex
public SObject getRecord(Id recordId)
{
    String objectType = recordId.getSObjectType().getDescribe().getName();
    String query = 'SELECT Id, Name FROM ' + objectType + ' WHERE Id = :recordId';
    return Database.query(query);
}

One method that fetches any record from any object. Note the object name comes from Salesforce’s own describe rather than user input, so there’s no injection risk from that concatenation, and the Id is properly bound.

Full details on describe results are in the dynamic Apex documentation. Worth a browse, there’s a lot in there.


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture