Skip to content

Instantly share code, notes, and snippets.

@capeterson
Created January 19, 2012 19:54
Show Gist options
  • Save capeterson/1642180 to your computer and use it in GitHub Desktop.
Save capeterson/1642180 to your computer and use it in GitHub Desktop.
getObjectType
public static Schema.SObjectType getObjectType(id subject){
if(subject == null)
return null;
Schema.SObjectType result;
string target = subject;
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
string keyPrefix;
for(Schema.SObjectType describe: gd.values() ){
keyPrefix = describe.getDescribe().getKeyPrefix();
if(keyPrefix != null && target.startsWith(keyPrefix)){
result = describe;
break; //no need to keep looking
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment