Activity Forums Salesforce® Discussions What is an easy (and fast) way to find a object named based on a parentid in Salesforce?

  • PRANAV

    Member
    September 30, 2016 at 5:34 am

    Hi Tanu,

    You can obtain the object type from an Id directly using the Id.getSObjectType method.

    And this code gives you the object name:

    public class KeyPrefix
    {
    private static Map<String,Schema.SObjectType> gd;
    private static Map<String, String> keyPrefixMap;
    private static Set<String> keyPrefixSet;

    private static void init() {
    gd = Schema.getGlobalDescribe();
    keyPrefixMap = new Map<String, String>{};
    keyPrefixSet = gd.keySet();
    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);
    String objectType = keyPrefixMap.get(tPrefix);
    return objectType;
    }
    }

    Now pass your id in Execute anonymous window like this to get the object name

    System.debug('@@@ '+ KeyPrefix.GetKeyPrefix('0012800000sa1Vs') );

    Hope this helps you.

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos