Dec 28, 2011

Fetching sObject Type From Record Id

"How I can get the sObject Type from Record Id?". In many requirements I do have 15-digit or 18-digit Id and I want to know the sObject to which the Id belongs to. Answer is using Global Describe we can get the sObject type.


Am getting too many emails from folks from Community Forums regarding this, so here is the generic method which will help all.


public class KeyPrefix
{
    // map to hold global describe data
    private static Map<String,Schema.SObjectType> gd;
    
    // map to store objects and their prefixes
    private static Map<String, String> keyPrefixMap;

    // to hold set of all sObject prefixes
    private static Set<String> keyPrefixSet;
    
    private static void init() {
        // get all objects from the org
        gd = Schema.getGlobalDescribe();
        
        // to store objects and their prefixes
        keyPrefixMap = new Map<String, String>{};
        
        //get the object prefix in IDs
        keyPrefixSet = gd.keySet();
        
        // fill up the prefixes map
        for(String sObj : keyPrefixSet)
        {
            Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
            String tempName = r.getName();
            String tempPrefix = r.getKeyPrefix();
            keyPrefixMap.put(tempPrefix, tempName);
        }
    }
    
    public static String GetKeyPrefix(String ObjId)
    {
        init() ;
        String tPrefix = ObjId;
        tPrefix = tPrefix.subString(0,3);
        
        //get the object type now
        String objectType = keyPrefixMap.get(tPrefix);
        return objectType;
    }
}


Now how you can use this? Simply save the class and pass your object Id in method "GetKeyPrefix" like this 


//a0090000002QGKu will be your object Id
System.debug('::::::: '+ KeyPrefix.GetKeyPrefix('a0090000002QGKu') );


And it will return you the object API name.

12 comments:

  1. Replies
    1. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai

      Delete
  2. How to write test class for this???

    ReplyDelete
  3. With Winter '13, there is a new getSObjectType method that will greatly reduce the number of script statements you need to execute. All you need to do to now is ObjId.getSObjectType()

    ReplyDelete
  4. just for code improvement, instead of:

    private static void init() {

    try:

    static {

    This technique called "static constructor" in Apex (Java).

    ReplyDelete
  5. Hi Ankit Arora,
    How to delete objects and fields using visualforce page?

    please help me............

    ReplyDelete
  6. You've really made my day today with this post.
    prostitution in London

    ReplyDelete
  7. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian

    court marriage in delhi ncr
    court marriage in delhi
    court marriage in noida
    court marriage in ghaziabad
    court marriage in gurgaon
    court marriage in faridabad
    court marriage in greater noida
    name change online
    court marriage in chandigarh
    court marriage in bangalore

    ReplyDelete
  8. We have seen many blog but this the great one, Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would like to request, wright more blog and blog post like that for us. Thanks you once agian

    birth certificate in delhi
    birth certificate in noida
    birth certificate in ghaziabad
    birth certificate in gurgaon
    correction in birth certificate
    marriage registration in delhi
    marriage certificate delhi
    how to change name in 10th marksheet
    marriage registration in ghaziabad
    marriage registration in gurgaon

    ReplyDelete