Li Jun
2022-04-19 2f183a6b0a83ec3f7d35375d5d25d200efc2a3e1
force-app/main/default/classes/NewAndEditBaseController.cls
@@ -198,6 +198,12 @@
        Map<String, Schema.SObjectField> fieldAPIToTypeMap = leadSchema.getDescribe().fields.getMap();
        Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeUntyped(leadJson);
        
        system.debug('enter Foo');
        string rtid = null;
        if (fieldValueMap.containsKey('RecordTypeId')) {
            rtid = String.valueOf(fieldValueMap.get('RecordTypeId'));
        }
        List<string> invalid_fields = GetInvalidFieldFromLayout(rtid,sobjectTypeValue);
        Boolean isClone = false;
        //2. Save Record Process
@@ -210,6 +216,12 @@
            
            for (String fieldAPI: fieldValueMap.keySet()) {
                system.debug('field API='+fieldAPI);
                if(invalid_fields.contains(fieldAPI)){
                    system.debug(fieldAPI+' is invalid');
                    continue;
                }
                if(!fieldAPIToTypeMap.containskey(fieldAPI)){
                    continue;
                }
@@ -312,4 +324,41 @@
        }
    }
    
    public static List<string> GetInvalidFieldFromLayout(string rtid, string sobject_name){
        List<string> ls = new List<string>();
        string[] only_type = new string[]{'QIS_Report__c'};
        if(!only_type.contains(sobject_name)){
            system.debug('not allow');
            return ls;
        }
        List<Metadata.LayoutSection> sections = MetaDataUtility.GetRecordTypePageLayout(rtid, sobject_name);
        if (sections == null) {
            System.debug('sections=null');
            return ls;
        }
        system.debug(Json.serialize(sections));
        for (Metadata.LayoutSection section : sections) {
            if (section.layoutColumns != null) {
                for (Metadata.LayoutColumn layoutColumn : section.layoutColumns) {
                    if(layoutColumn.layoutItems != null){
                        for (Metadata.LayoutItem item : layoutColumn.layoutItems) {
                            System.debug(item);
                            if(item.field==null)continue;
                            if (item.behavior == Metadata.UiBehavior.READONLY  ) {
                                ls.add(item.field);
                            }
                        }
                    }
                }
            }
        }
        return ls;
    }
}