public without sharing class SObjectHelper { /** * Copy every field values from from_sobj to to_sobj, if the field is in to_sobj and the value is not null */ public static void MergeValue(Sobject to_sobj,Sobject from_sobj) { CopyTo(to_sobj,from_sobj,true); } /** * Copy every field values from from_sobj to to_sobj */ public static void OverwriteValue(Sobject to_sobj,Sobject from_sobj) { CopyTo(to_sobj,from_sobj,false); } static void CopyTo(Sobject to_sobj,Sobject from_sobj, Boolean skip_from_null) { Map to_sobj_map = GetFields(to_sobj.getSObjectType().getDescribe().getName()); Map from_sobj_map = GetFields(from_sobj.getSObjectType().getDescribe().getName()); for (string field : from_sobj_map.keySet()) { object obj = from_sobj.get(field); object obj1 = to_sobj.get(field); if (obj != obj1 && to_sobj_map.containsKey(field) && to_sobj_map.get(field).getDescribe().isUpdateable()) { if (obj != null || !skip_from_null) { System.debug('obj='+obj); System.debug('obj1='+obj1); to_sobj.put(field, obj); } } } } private static Map> sobject_field_map = new Map>(); public static Map GetFieldMap(string obj_name) { Map fm = null; if(string.isBlank(obj_name))return fm; if(sobject_field_map.containsKey(obj_name)) { fm = sobject_field_map.get(obj_name); } else { fm = GetFields(obj_name); if (fm != null) { sobject_field_map.put(obj_name, fm); } } return fm; } /* private static Map> sobject_common_fields = new Map>(); // get sobject fields that not be assigned in FieldPermission, they were stardard fields public static Set GetCommonFields(string obj_name) { Set fields = null; if(string.isBlank(obj_name))return fields; if(sobject_common_fields.containsKey(obj_name)) { fields = sobject_common_fields.get(obj_name); } else { Map all_fields = GetFieldMap(obj_name); Id profile_id = UserUtility.GetSysAdminProfileId(); List lfp = [SELECT Id, ParentId, SobjectType, Field, PermissionsEdit, PermissionsRead, SystemModstamp FROM FieldPermissions where parentId in (select id from permissionset where PermissionSet.Profile.Id = :profile_id) and sobjecttype = :obj_name ]; Set admin_s = new Set(); for(FieldPermissions fp : lfp) { if(fp.PermissionsRead) { admin_s.add(fp.Field.replace(fp.SobjectType+'.','')); } } // if a field not in admin's permission, the field is a common(standard) field for (string f : all_fields.keySet()) { if (!admin_s.contains(f) && !f.endsWith('__c')) { if (fields == null) { fields = new Set(); } fields.add(f); } } if (fields != null) { sobject_common_fields.put(obj_name, fields); } } return fields; } public static Set GetCurrentProfileAccessableFields(string obj_name) { return GetProfileAccessableFields(UserInfo.getProfileId(), obj_name); } private static Map>> profile_field_permission_buffer = new Map>>(); public static Set GetProfileAccessableFields(Id profile_id, string obj_name) { List lfp = null; if(!profile_field_permission_buffer.containsKey(profile_id) || !profile_field_permission_buffer.get(profile_id).containsKey(obj_name)){ lfp = [SELECT Id, ParentId, SobjectType, Field, PermissionsEdit, PermissionsRead, SystemModstamp FROM FieldPermissions where parentId in (select id from permissionset where PermissionSet.Profile.Id = :profile_id) and sobjecttype = :obj_name ]; system.debug(string.format('profile {0} object {1} updated ', new object[]{profile_id,obj_name,lfp.size()})); Map> temp = null; if(profile_field_permission_buffer.containsKey(profile_id)) { temp = profile_field_permission_buffer.get(profile_id); } else{ temp = new Map>(); profile_field_permission_buffer.put(profile_id,temp); } temp.put(obj_name,lfp); } else{ lfp = profile_field_permission_buffer.get(profile_id).get(obj_name); } Set ls = new Set(); for(FieldPermissions fp : lfp) { if(fp.PermissionsRead) { ls.add(fp.Field.replace(fp.SobjectType+'.','')); } } Set css = GetCommonFields(obj_name); if (css != null) { ls.addAll(css); } return ls; }*/ public static Map GetFieldInfos(string obj_name) { Map fields_map = SObjecthelper.GetFieldMap(obj_name); if(fields_map == null) { return null; } Map res = new Map(); Map result = new Map(); FieldInfo temp = null; for(string s : fields_map.keySet()) { Schema.DescribeFieldResult dfr = fields_map.get(s).getDescribe(); temp = new FieldInfo(); temp.Name = dfr.getName(); temp.Label = dfr.getLabel(); // resolve 'Business Phone' bug if(obj_name.toLowerCase() == 'contact' && temp.Name =='Phone' && temp.Label =='Business Phone'){ temp.Label = 'Phone'; } Schema.DisplayType dt = dfr.getType(); temp.TypeEnumName = dt.name(); //temp.DisplayType = dt; if((dt == schema.DisplayType.MULTIPICKLIST) || (dt == schema.DisplayType.PICKLIST) ) { temp.Options = new List