Wrapper class:-
How to get Object name by passing record ID:
How to get Object name by passing record ID:
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)
{
String specialChars = '+-&|^|@() *_!$%=?.,<>{}[]|#/\\';
for(integer i=0; i<specialChars.split('|').size(); i++)
ObjId= ObjId.replace(specialChars.split('|')[i], '');
init() ;
String tPrefix = ObjId;
tPrefix = tPrefix.subString(0,3);
//get the object type now
String objectType = keyPrefixMap.get(tPrefix);
return objectType;
}
}
{
// 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)
{
String specialChars = '+-&|^|@() *_!$%=?.,<>{}[]|#/\\';
for(integer i=0; i<specialChars.split('|').size(); i++)
ObjId= ObjId.replace(specialChars.split('|')[i], '');
init() ;
String tPrefix = ObjId;
tPrefix = tPrefix.subString(0,3);
//get the object type now
String objectType = keyPrefixMap.get(tPrefix);
return objectType;
}
}
Call Above class from Developer console:
System.debug('::::::: '+ KeyPrefix.GetKeyPrefix('record id here') );
No comments:
Post a Comment