UTIL CLASS
using string literals
Literal strings must be enclosed in single quotation marks. double quotation marks are reserved for enclosing attribute values, and must be escaped in strings. Use the “\” character to escape strings. Favorite
sObjectUtils class, utility
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
global with sharing class sObjectUtils { /** * 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; } public static sObject getAllFields(id subject){ Schema.SObjectType objectType = getObjectType(subject); Map<String, Schema.SObjectField> fields = objectType.getDescribe().fields.getMap(); string queryString = 'SELECT '; for(Schema.sObjectField field:fields.values()){ queryString += ' '+field.getDescribe().getName()+', '; } queryString = queryString.substring(0, queryString.length()-2)+' FROM '+objectType.getDescribe().getName()+' WHERE id = \''+subject+'\' LIMIT 1'; sObject result = Database.query(queryString); return result; } } |
Favorite