Li Jun
2022-04-19 2f183a6b0a83ec3f7d35375d5d25d200efc2a3e1
force-app/main/default/classes/NewAndEditBaseController.cls
@@ -6,6 +6,7 @@
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String sobjectTypeValue {private set; get;}
    public Boolean isNewMode{set;get;}
    public Boolean isCloneMode{protected set;get;}
    public String rtTypeId {get; set;}
    public String AWSDataId{set;get;}
    public String CurrentUserId{private set;get;}
@@ -53,7 +54,7 @@
    public List<String> LookUpOverrideFields{get;private set;}
    public string LookUpOverrideFieldsMapJson{get; set;}
    public string recordId{get;private set;}
    public NewAndEditBaseController(){
        ApiPrefix = 'PIBackApi';
        AWSToSobjectNonEncryptedMap = new Map<string,string>();
@@ -71,8 +72,12 @@
        system.debug('obj='+sobjectTypeValue);
        
        isNewMode = true;
        isCloneMode = false;
        List<Sobject> lso = Database.query('select id from RecordType where SobjectType = :sobjectTypeValue');
        Map<string,string> mso = ApexPages.currentPage().getParameters();
        if(mso != null && mso.containsKey('newclone')){
            isCloneMode = true;
        }
        if(obj.Id != null){
            recordId = obj.Id;
            isNewMode = false;
@@ -186,13 +191,21 @@
        string sobjectTypeValue = sobj.getSObjectType().getDescribe().getName();
        System.debug('sobjectTypeValue:'+sobjectTypeValue+' Info:' + JSON.serialize(leadJson));
        System.debug('json length='+leadJson.length());
        System.debug('leadJson---------'+leadJson);
        System.debug('isNew---------'+isNew);
        //1. Prepare the payload for  Lead
        Schema.SObjectType leadSchema = schemaMap.get(sobjectTypeValue);
        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
        String status = 'success';    
        Response resp = new Response();
@@ -200,9 +213,15 @@
        Savepoint sp = Database.setSavepoint();
        Sobject leadInfo = sobj;
        try{
            System.debug('abcde');
            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;
                }
@@ -215,6 +234,11 @@
                    if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){
                        fieldValue = fieldValue.replace('T',' ');
                        leadInfo.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
                    //20220405 By ChenYanan Start
                    }else if(String.isNotBlank(fieldValue))  {
                        fieldValue = fieldValue.replace('/', '-') + ':00';
                        leadInfo.put(fieldAPI, Datetime.valueOf(fieldValue));
                    //20220405 By ChenYanan End
                    }else{
                        leadInfo.put(fieldAPI, null);
                    }                    
@@ -228,22 +252,28 @@
            }
            system.debug('for (String fieldAPI: fieldValueMap.keySet()) end');
            awsDataId = (String)leadInfo.get('AWS_Data_Id__c');
            if (string.isBlank(awsDataId)) {
                throw new DMLException('更新时AWS_Data_Id__c不能为空');
            }
            System.debug('awsDataId = ' + awsDataId);
            Sobject[] sobjects = Database.query('select id from '+sobjectTypeValue+' where AWS_Data_Id__c =:awsDataId');
            
            if(isNew){
            if(!isNew){
                isClone = sobjects.size() == 0;
            }
            System.debug('isNew---------'+isNew);
            if(isNew || isClone){
                System.debug('leadInfozhj = ' + leadInfo);
                if(!Test.isRunningTest()){
                    insert leadInfo;
                }
            }else{
                System.debug('into update');
                awsDataId = (String)leadInfo.get('AWS_Data_Id__c');
                if (string.isBlank(awsDataId)) {
                    throw new DMLException('更新时AWS_Data_Id__c不能为空');
                }
                System.debug('awsDataId = ' + awsDataId);
                Sobject[] leads = Database.query('select id from '+sobjectTypeValue+' where AWS_Data_Id__c =:awsDataId');
                System.debug('leads[0].id = ' + leads[0].id);
                leadInfo.put('Id',leads[0].id);//For testing;
                System.debug('sobjects[0].id = ' + sobjects[0].id);
                leadInfo.put('Id',sobjects[0].id);//For testing;
                if(!Test.isRunningTest()){
                    update leadInfo;
                }
@@ -273,7 +303,7 @@
            System.debug(e.getDmlMessage(index));
            System.debug(e.getDmlStatusCode(index));
            System.debug(e.getDmlType(index));
         system.debug(e.getMessage());
            system.debug(e.getMessage());
            system.debug(e.getStackTraceString());
            System.debug('into catch'+e.getMessage());
@@ -294,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;
    }
}