Posted By

d3developer on 08/10/10


Tagged

describe id Apex Sobject Sobjecttype


Versions (?)

@ca_peterson SObject initialization from Id


 / Published in: Other
 

  1. #
  2. * This method accepts the ID of any object type and returns the full name, including prefix of the sObject
  3. #
  4. * type to which it belongs.
  5. #
  6. * @author cpeterson
  7. #
  8. **/
  9. #
  10. public static Schema.SObjectType getObjectType(id subject){
  11. #
  12. Schema.SObjectType result;
  13. #
  14. string target = subject;
  15. #
  16. Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
  17. #
  18. string keyPrefix;
  19. #
  20.  
  21. #
  22. for(Schema.SObjectType describe: gd.values() ){
  23. #
  24. keyPrefix = describe.getDescribe().getKeyPrefix();
  25. #
  26. if(keyPrefix != null && target.startsWith(keyPrefix)){
  27. #
  28. result = describe;
  29. #
  30. break; //no need to keep looking
  31. #
  32. }
  33. #
  34. }
  35. #
  36. return result;
  37. #
  38. }

Report this snippet  

You need to login to post a comment.