How to get Salesforce Object key prefix

Salesforce Logo

Sharing is Caring

In Salesforce, the first three characters of a record Id are the “Object Id Prefix”. There’s a lot of standard objects that have published Id Prefixes, I’ve posted a blog article about the Standard Object Id Prefixes.

If you run into an Id with a prefix that isn’t on this list, and you have the ability to execute anonymous apex, you can find out what the object is with 2 lines of code:

Id testId = ’04uf00000009P95′;
system.debug(testId.getSObjectType());

To get the Object prefix for a standard or custom object, you can use code like this:

public static String getObjectKeyPrefix(String objectName){
    Schema.sObjectType objectType = Schema.getGlobalDescribe().get(objectName);
    return objectType.getDescribe().getKeyPrefix();
}

And then you can simply call this static method at any point in your code to determine the ObjectKeyPrefix.

Sharing is Caring
,