@ca_peterson SObject initialization from Id


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.