Apex: Find Object Type from Record ID Prefix

I get to work on objects in various orgs. It’s not easy (if at all possible) to remember entity type by just looking at Record ID Prefix. salesforce.com provides a Standard Record ID Prefix Decoder list. This still means you need to have this page handy to find those objects. Of course this list also cannot help with custom objects.

In my pursuit to find an answer, I came across piece of code that was answer to this exact problem. The answer uses iteration on metadata through Schema.getGlobalDescribe(). As it loops through all sObjects in the map, it will continue to check for prefix that we need to find. Once there, it will return the object name. By no means this is an optimized solution as it goes through loop of all sObjects till it can find the object. But this is better than having to go through other ways to find same information. Of course, if you come across an easier way then please share.

public class SchemaGlobalDescribe{
  public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix){
    String objectName = '';
    try{
      //Get prefix from record ID
      //This assumes that you have passed at least 3 characters
      String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3);
     
      //Get schema information
      Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
     
      //Loop through all the sObject types returned by Schema
      for(Schema.SObjectType stype : gd.values()){
        Schema.DescribeSObjectResult r = stype.getDescribe();
        String prefix = r.getKeyPrefix();
        System.debug('Prefix is ' + prefix);
      
        //Check if the prefix matches with requested prefix
        if(prefix!=null && prefix.equals(myIdPrefix)){
          objectName = r.getName();
          System.debug('Object Name! ' + objectName);
          break;
        }
      }
    }catch(Exception e){
      System.debug(e);
    }
    return objectName;
  }
}

Now we can easily use this in developer console to check object name whenever we want. Of course you can take it a step further and create a visualforce page, add a search box and make it available to all developers in your org so they can easily use it (if needed).

String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.debug(objectName);

Oh wait, we have written the class, we have tested it but we are missing an important step. Remember? Yes, “Code Coverage”. That’s an important point that we shouldn’t miss. This is important not just to be able deploy the code but it’s good development practice to make sure we test our code. Following salesforce page provides information on testing best practices.

So, I have written very basic test class to cover the code written above. You can add more functionality to your class and then expand on these basic tests.

@isTest
private class SchemaGlobalDescribeTests{
    @istest
    private static void testMethodPositive(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
        System.assertEquals(objectName,'Case');
    }
    @isTest
    private static void testMethodNegative(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
        System.assertNotEquals(objectName,'Account');
    }
    @isTest
    private static void testMethodNull(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('101');
        System.assertEquals(objectName,'');
    }
    @isTest
    private static void testMethodException(){
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('10');
        System.assertEquals(objectName,'');
    }
}

That’s that and now we can easily figure out object name from Record ID prefix.

Please share your thoughts, questions, concerns or constructive criticism.

 

Leave a Reply