public without sharing class MetaDataUtility { public static Map schemaMap = Schema.getGlobalDescribe(); public static List GetRecordTypePageLayout(string record_type_id,string objectType){ System.debug('record_type_id = ' + record_type_id); System.debug('objectType = ' + objectType); System.debug('UserInfo.getProfileId() = ' + UserInfo.getProfileId()); Map mso = null; string layout_name = GetRecordTypePageLayoutName(record_type_id,objectType,UserInfo.getProfileId()); System.debug('layout_name = ' + layout_name); List temp = GetLayoutSections(objectType,layout_name); System.debug('temp = ' + temp); List layoutSectionList = new List(); //7.20 张赫阳 判断当前用户是否是管理员 start Profile UserProfile = [SELECT Name,PermissionsModifyAllData FROM Profile WHERE Id =: UserInfo.getProfileId() LIMIT 1]; String ByPassProfileName = System.Label.AdminProfileName; //7.20 张赫阳 判断当前用户是否是管理员 end for(Metadata.LayoutSection section : temp){ boolean a = false; for( Metadata.LayoutColumn c: section.layoutColumns){ //system.debug(c); if(c.layoutItems != null && c.layoutItems.size() > 0 ){ a = true; //7.20 张赫阳 添加permissonset start for(Metadata.layoutItem item : c.layoutItems){ if(UserProfile.PermissionsModifyAllData){ if((item.behavior != Metadata.UiBehavior.Readonly || GetPermission(item.field, objectType))) { if(item.behavior == Metadata.UiBehavior.Readonly){ System.debug(item.field + item.behavior); item.behavior = Metadata.UiBehavior.Readonly; System.debug('发生权限变动==============》' + item.behavior); } } } if(ByPassProfileName == UserProfile.Name){ if(item.behavior == Metadata.UiBehavior.Readonly){ item.behavior = Metadata.UiBehavior.Edit; System.debug('发生权限变动==============》' + item.behavior); } } // System.debug(item.field + item.behavior); } //7.20 张赫阳 添加permissonset end } } if(a){ layoutSectionList.add(section); } } System.debug('layoutSectionList' + layoutSectionList); return layoutSectionList; } //7.20 张赫阳 添加permissonset start public static Boolean GetPermission(String fieldName, String objectType){ Schema.SObjectType sobjectSchema = schemaMap.get(objectType); Map fieldMap = sobjectSchema.getDescribe().fields.getMap(); system.debug(fieldMap.get(fieldName).getDescribe().isUpdateable()); return fieldMap.get(fieldName).getDescribe().isUpdateable(); } //7.20 张赫阳 添加permissonset end /*[{ "attributes": { "type": "Layout", "url": "/services/data/v53.0/tooling/sobjects/Layout/00h10000009iAb5AAE" }, "Id": "00h10000009iAb5AAE", "Name": ".客户人员レイアウト", "TableEnumOrId": "01I10000000er3hEAA", "LayoutType": "Standard" }] */ public static string GetTableOrEnumId(string objectType){ Map mso = null; string resp = null; string table_or_enum_id = objectType; if(objectType.endsWith('__c')){ resp = ToolingQuery('SELECT id,DeveloperName from CustomObject where DeveloperName =\''+objectType.replace('__c', '')+'\''); if(resp == null){ system.debug('resp is not ok'); return null; }else{ mso = (Map)Json.deserializeUntyped(resp); if(integer.valueOf(mso.get('size')) > 0){ table_or_enum_id = string.valueOf(((Map)(((List)mso.get('records'))[0])).get('Id')); }else{ system.debug('no records'); return null; } } } return table_or_enum_id; } public static List GetAllPageLayout(string objectType){ string resp = null; Map mso = null; string table_or_enum_id = GetTableOrEnumId(objectType); if(string.isBlank(table_or_enum_id)){ return new List(); } resp = ToolingQuery('SELECT id,name,TableEnumOrId,LayoutType FROM Layout where TableEnumOrId = \''+table_or_enum_id+'\''); if(resp == null){ system.debug('Layout where TableEnumOrId='+table_or_enum_id+' is null'); return null; }else{ mso = (Map)Json.deserializeUntyped(resp); if(integer.valueOf(mso.get('size')) > 0){ return (List)mso.get('records'); }else{ system.debug('no records'); return new List(); } } } public static string GetRecordTypePageLayoutName(string record_type_id,string objectType, string profile_id){ if(!string.isBlank(objectType)){ List lso = [SELECT Id, Name, DeveloperName, SobjectType, IsActive, Description, BusinessProcessId FROM RecordType where SobjectType = :objectType]; if(lso.size()==0){ return string.valueOf(((Map)(GetAllPageLayout(objectType)[0])).get('Name')); } } // 01210000000QfWdAAK string query = 'SELECT Layout.Name, Layout.TableEnumOrId, ProfileId, Profile.Name, RecordTypeId FROM ProfileLayout where id!=null '; if(!string.isBlank(record_type_id)){ query += ' and RecordTypeId = \''+record_type_id+'\''; } if(!string.isBlank(objectType)){ query += ' and TableEnumOrId = \''+ GetTableOrEnumId(objectType) +'\''; } if(!string.isBlank(profile_id)){ query += ' and ProfileId = \''+profile_id+'\''; } //query += ' and Layout.Name = \''+'中文区块'+'\''; query += ' order by LastModifiedDate desc '; system.debug('query='+query); string s = ToolingQuery(query); if(string.isBlank(s)){ system.debug('s is blank'); return null; }else{ Map mso = (Map)JSON.deserializeUntyped(s); if(integer.valueOf(mso.get('size')) > 0){ List records = ((List)mso.get('records')); mso = (Map)(records[0]); return string.valueOf(((Map)(mso.get('Layout'))).get('Name')); }else{ return null; } } } public static string ToolingQuery(string query){ system.debug('query='+query); String baseURL = 'callout:SF_Rest_API/services/data/v41.0/tooling/query?q='+ query.replace(' ', '+'); System.debug('baseURL = ' + baseURL); HttpResponse resp = null; HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setHeader('Authorization', 'Bearer ' + UserInfo.getsessionid()); //deloitte-zhj 20231117 PIPL //req.setEndpoint(baseURL); req.setEndpoint(baseURL); Http client = new Http(); resp = client.send(req); system.debug('resp = ' + resp); system.debug(resp.getStatus()); system.debug(resp.getStatusCode()); if(resp.getStatus() == 'OK'){ string s = resp.getBody(); system.debug(resp.getBody()); return s; }else{ system.debug('status is not ok,error:'+resp.getBody()); return null; } } public static List GetLayoutSections(string object_name, string layout_name){ List componentNameList = new List{object_name+'-'+layout_name}; //通过Metadata.Operations.retrieve获取metadata //Metadata.Layout -> Metadata.LayoutSection -> Metadata.LayoutColumn objects -> Metadata.LayoutItem objects List componentList = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, componentNameList); if(componentList?.size() > 0){ Metadata.Layout layout = (Metadata.Layout) componentList.get(0); List layoutSectionList = layout.layoutSections; return layoutSectionList; } else{ return null; } } }