游畅
2022-04-28 a25138b23fdaa05197fb25fdbb3202f122235611
force-app/main/default/classes/InquiryFormHandler.cls
@@ -27,11 +27,12 @@
    }
    protected override void afterInsert() {
        //shareToOSCM();
        //FSE主担当共享
        shareToFSEMain();
    }
     protected override void afterUpdate() {
        //shareToOSCM();
        shareToFSEMain();
    }
     //Before処理
    private void beforeExecute() {
@@ -54,7 +55,9 @@
                nnObj.HospitalName__c = null;     //医院名
                nnObj.Hospital__c = null;         //医院
            }
        }
        if (depIDList.size()>0) {
            accList  = [Select Id,OwnerId,
                            Hospital__c,Hospital__r.Name,
@@ -75,6 +78,8 @@
                nObj.HospitalName__c = accMap.get(nObj.Hospital_Name__c).Hospital__r.Name;        //医院名
                nObj.Hospital__c = accMap.get(nObj.Hospital_Name__c).Hospital__c;                 //医院
            }
            
           //产品信息的拼接
            if (String.isNotBlank(nObj.Product1__c) && String.isNotBlank(nObj.Product1_Manual__c)){
@@ -99,8 +104,121 @@
            }
            
            nObj.Product1_Manual__c = '';
             //20220419 you SWAG-CBUB2W start
            //公式考文本  给FSE担当赋值
            System.debug(nObj.FSE_Owner_id__c+'==123=='+nObj.FSE_Owner__c);
            if(nObj.FSE_Owner_id__c != nObj.FSE_Owner__c){
                nObj.FSE_Owner__c = nObj.FSE_Owner_id__c;
            }
            if(Trigger.isInsert && (nObj.Request1__c.indexOf('服务对应') !=-1 || nObj.ServiceCorrespond__c ==true)){
                nObj.Service_Status__c ='01.未跟进';
            }
            if(Trigger.isUpdate){
                if((nObj.Request1__c != oldMap.get(nObj.Id).Request1__c && nObj.Request1__c.indexOf('服务对应') !=-1) || (nObj.ServiceCorrespond__c != oldMap.get(nObj.Id).ServiceCorrespond__c &&  nObj.ServiceCorrespond__c ==true)){
                    if(String.isBlank(nObj.Service_Status__c)){
                       nObj.Service_Status__c ='01.未跟进';
                    }else{
                    }
                }else if( nObj.Request1__c.indexOf('服务对应') ==-1 && nObj.ServiceCorrespond__c ==false){
                       nObj.Service_Status__c ='';
                }
            }
            //20220419 you SWAG-CBUB2W end
        }
    }
    //20220419 you SWAG-CBUB2W start
    private void shareToFSEMain() {
         //存放用于新增的共享数据
         List<Inquiry_form__Share> insertList = new List<Inquiry_form__Share>();
         //存放(产品咨询单id,共享对象)
         Map<Id,Inquiry_form__Share> insertMap = new Map<Id,Inquiry_form__Share>();
         //存放最后需要新增的共享数据
         List<Inquiry_form__Share> lastInsertList = new List<Inquiry_form__Share>();
         //存放已有的相同的共享原因的数据
         List<Id> deleteTargetAOIdList = new List<Id>();
         //存放用于的id
         List<Id> userIdList = new List<Id>();
         // String rowCause = 'Manual';
         //新增一个共享原因
         String rowCause = Schema.Inquiry_form__Share.RowCause.FSE_Owner_c_User__c;
         System.debug('rowCause:'+rowCause);
         String ownerCause = 'Owner';
         //Apex共有の理由名OCSM_Owner_c_User
        for(Inquiry_form__c nObj : newList) {
             Inquiry_form__c oObj = null;
             if (oldMap != null && oldMap.containsKey(nObj.Id)) {
                     oObj = oldMap.get(nObj.Id);
             }
             if ( nObj.FSE_Owner__c != null && (oObj == null || oObj.FSE_Owner__c != nObj.FSE_Owner__c)) {
                 Inquiry_form__Share aos = new Inquiry_form__Share(
                         RowCause = rowCause,
                         ParentId = nObj.Id,
                         UserOrGroupId = nObj.FSE_Owner__c,
                         AccessLevel = 'Edit');
                 //存放要新增的共享数据
                 insertList.add(aos);
                 System.debug('key:'+nObj.Id);
                 //存放(产品咨询单id,共享对象);
                 insertMap.put(nObj.Id,aos);
                 //存放用户id 用作检索条件
                 userIdList.add(nObj.FSE_Owner__c);
             }
             if(oObj != null && oObj.FSE_Owner__c != nObj.FSE_Owner__c){
                deleteTargetAOIdList.add(nObj.Id);
             }
        }
         System.debug('insertList:'+insertList);
         System.debug('insertMap1:'+insertMap);
         System.debug('IDlIST:'+deleteTargetAOIdList);
         // 先 Delete 后 Insert
         if (deleteTargetAOIdList.size() > 0) {
             List<Inquiry_form__Share> deleteList = [SELECT Id
                 FROM Inquiry_form__Share
                 WHERE RowCause = :rowCause
                   AND ParentId IN :deleteTargetAOIdList
             ];
             delete deleteList;
         }
         //判断需要共享的人  是不是创建人 如果是则说明有一条这个人的owner数据 则不新增
         if (insertMap!= null) {
             List<Inquiry_form__Share> ownerList = [SELECT Id,ParentId,UserOrGroupId
                 FROM Inquiry_form__Share
                 WHERE RowCause = :ownerCause
                   AND ParentId IN :insertMap.keySet()
                   AND UserOrGroupId IN :userIdList
             ];
             System.debug('ownerList:'+ownerList);
             if(ownerList.size() > 0){
                 for( Inquiry_form__Share  inq:ownerList){
                     String id = String.valueOf(inq.ParentId);
                     System.debug('id:'+id);
                     if(insertMap.containsKey(id)){
                         insertMap.remove(inq.ParentId);
                     }
                 }
             }
         }
         System.debug('insertMap2:'+insertMap);
         if(insertMap != null){
                 for(Inquiry_form__Share inquiry : insertMap.values()){
                     lastInsertList.add(inquiry);
                 }
         }
         System.debug('共享内容:' +lastInsertList);
         if(lastInsertList.size() > 0){
             insert lastInsertList;
         }
     }
     //20220419 you SWAG-CBUB2W end
    //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
    // private void getContactInformation(){
@@ -222,4 +340,48 @@
    //     }
        
    // }
     @TestVisible private static void test() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;//可以多写点
    }
}