Get three digit key of an Object using Apex

In Salesforce every records, objects, fields and RecordType etc has an ID which is used to uniquely identify the component. In each id the first three digit(object key prefix) will represent the object type.


Format of the Salesforce id:            

ID : 003D0000001ah2A

003 D 0000001ah2A
Object ID Prefix Server ID Identifier

Below link will have a list of standard objects key prefix.

There is a better way to get the object id prefix. Below is the apex code to get the object id prefix.

Apex code:

Schema.DescribeSObjectResult  accountDescResult=Account.sObjectType.getDescribe();
String accountKeyPrefix=accountDescResult.getKeyPrefix();
System.debug('***key prefix****'+accountKeyPrefix);


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.


Rate this article if its useful to you, Please comment if you have any suggestions.

No votes yet