Created
May 24, 2011 21:37
-
-
Save fractastical/989758 to your computer and use it in GitHub Desktop.
@ca_peterson SObject initialization from Id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
* This method accepts the ID of any object type and returns the full name, including prefix of the sObject | |
# | |
* type to which it belongs. | |
# | |
* @author cpeterson | |
# | |
**/ | |
# | |
public static Schema.SObjectType getObjectType(id subject){ | |
# | |
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