How to get KeyPrefix of sObject in Salesforce

Every record in Salesforce has an ID which is used to uniquely identify the record. Each ID value is globally unique. The first three digits represent the object type.

For example, the Account object uses the 001 prefix; the Note object uses the 002 prefix, Contact uses 003, Users 005, Opportunity 006 etc.

We can get these prefix values dynamically as well using Apex. To do this, use the below Apex code–

The key prefixes for all the standard objects remain the same.

You can use the above Apex code to get the key prefix of your custom objects. To do this just replace Account with the name of your custom object to obtain the key prefix of that custom object.