Tuesday 15 December 2015

Record ID Prefix | Object Type from Record ID Prefix | Prefix of an Object in Salesforce.


We know there are two types of record-id are present in salesforce (18 digit -- Case Insensitive,15 digit -- Case Sensitive). Only 3 digit of ids represent object type .The following is a list of the Salesforce Standard Object ID prefixes


Object
Prefix
Account
001
NOTE
002
Contact
003
User
005
Opportunity
006
Activity
007


 Please check below post for more detail

If you want to get the Prefix of any object . Please try below code.


String keyPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
System.debug('PREFIX--' + keyPrefix );


If you want to get the Object Name from RecordId then please try below code.


public class SchemaGlobalDescribeToGetObjectName
{
    public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix)
 {
  String objectName = '';
        try
  {
            String IdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3); 
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
            for(Schema.SObjectType stype : gd.values())
   {
                Schema.DescribeSObjectResult r = stype.getDescribe();
                String prefix = r.getKeyPrefix();
                if(prefix!=null && prefix.equals(IdPrefix))
    {
                    objectName = r.getName();
                    System.debug('Object Name! ' + objectName);
                    break;
                }
            }
        }catch(Exception e){
            System.debug(e);
        }
        return objectName;
    }
}


If you want to Get the Object prefix by Workbench then please try below step :-

The easiest way to find out which key prefix maps to which object is by using a tool like the workbench . After you log into workbench, go to and pick an object from the pick list.

Step 1 :- Login in to Workbench

Step 2:- Info > Standard & Custom Objects 


Step 3:- Then select your object





Thanks
Amit Chaudhary

No comments:

Post a Comment