Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active December 12, 2015 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcon/4693281 to your computer and use it in GitHub Desktop.
Save pcon/4693281 to your computer and use it in GitHub Desktop.
// Alternate way to get the name from a given Id
String objName = objId.getSObjectType().getDescribe().getName();
/**
* Gets a map of the prefix for an object to the object name
*
* @return A map of id prefix to object name
*/
public static Map<String, String> getObjectToPrefixMap() {
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<String,String> keyPrefixMap = new Map<String,String>{};
Set<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);
}
return keyPrefixMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment