Get Key Prefix of Salesforce Object

In my previous post, I have explained to you how to use the Salesforce Object Key Prefix to save your time. If you haven’t seen that yet the please refer to this link Salesforce Object Key Prefix Hack.

In this post, I will explain how to get the key prefix for a custom object.

Every custom object in Salesforce has its unique key prefix, using which they are uniquely identified. We will be getting the key prefix for one of the custom objects in our SF organization.

Let’s say we have a custom object “Invoices__c”, and we want the Key Prefix of this object. By using apex we can get the prefix of that object in the developer console.


String keyPrefix = Invoices__c.SObjectType.getDescribe().getKeyPrefix();
System.Debug('Key Prefix: ' + keyPrefix);

Note: You can get the key prefix for any of your custom object. You just need to place the object name for which you want the prefix.