How to get Id of the Object

Salesforce allows you to get the Key Prefix.

Key Prefix is 3 character unique key for each Object. There are two ways to find the Key prefix.

1) using schema.getGlobalDescribe()

For(Schema.SObjecttype obj: Schema.getGlobalDescribe().Values()){

System.debug(obj.getDescribe());
System.debug(obj.getDescribe().getKeyPrefix());

}

 

2) Every record Id first 3 letters is a Object KeyPrefix;

Contact cc: [Select id from Contact limit 1];
String keyPrefix = String.valueOf(cc.id).substring(0,3);

 

Leave a comment