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–

    Schema.DescribeSobjectResult r = Account.getSobjectType().getDescribe();nString keyPrefix = r.getKeyPrefix();

    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.

    Leave a Reply

    Your email address will not be published. Required fields are marked *