buli
2023-07-14 36d15f189de2e83ce2576715dac30c3c260388dd
force-app/main/default/triggers/ContactHpDeptUpd.trigger
@@ -1,14 +1,19 @@
//医療従事者の診療科(兼務先)を変更の場合、戦略科室・病院も修正
trigger ContactHpDeptUpd on Contact (before insert, before update) {
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
    List<String> accIds = new List<String>();
    List<String> FirstNameList = new List<String>();
    List<String> LastnameList = new List<String>();
    List<String> LastnameEncryptedList = new List<String>();
    List<id> Hospital_id_list = new List<id>();
    List<id> ACCOUNT_id_list = new List<id>();
    Map<String,Contact> mapCon = new Map<String,Contact>();
//***************************************INSERT 2016-12-14 By ZDF START*************************************
//          增加“新增客户人员入例规则”
//***************************************INSERT 2016-12-14 By ZDF START*************************************
//2022-02-10    PI改造,用LastName_Encrypted__c替代原有的Name作为mapCon映射的键
    for(Contact a : Trigger.new) {
        if (Trigger.isInsert
                || (Trigger.isUpdate
@@ -21,9 +26,12 @@
        if(Trigger.isInsert&&a.Ignore_Same_Name__c==false){
            FirstNameList.add(a.firstname);
            LastnameList.add(a.Lastname);
            //zhj MEBG新方案改造 2022-11-28 start
            //LastnameEncryptedList.add(a.LastName_Encrypted__c);
            ACCOUNT_id_list.add(a.Accountid);
            mapCon.put((a.firstname+a.Lastname),a);
            //mapCon.put(a.LastName_Encrypted__c,a);
            system.debug('========1111111');
            //zhj MEBG新方案改造 2022-11-28 end
        }
    }
@@ -49,49 +57,56 @@
                Hospital_id_list.add(   acc.Id  );
            }
        }
        List<Contact> CntingList = [    select
                                                id,
                                                Name,
                                                Department__c,
                                                Account.Parent.Name,
                                                Account.name,
                                                Account.Parent.Parent.Name
                                        from
                                                Contact
                                        where
                                                firstname  in:FirstNameList
                                        and
                                                Lastname   in:LastnameList
                                        and
                                            (
                                                Account.Parent.Parentid  in:Hospital_id_list
                                        or
                                                Account.Parentid  in:Hospital_id_list
                                        or
                                                Account.id  in:Hospital_id_list
                                            )
                                ];
        //zhj MEBG新方案改造 2022-11-28 start
        // List<Contact> CntingList = [    select
        //                                         id,
        //                                         Name,
        //                                         Department__c,
        //                                         Account.Parent.Name,
        //                                         Account.name,
        //                                         Account.Parent.Parent.Name
        //                                 from
        //                                         Contact
        //                                 where
        //                                 //         firstname  in:FirstNameList
        //                                 // and
        //                                 //         Lastname   in:LastnameList
        //                                 LastName_Encrypted__c in : LastnameEncryptedList
        //                                 and
        //                                     (
        //                                         Account.Parent.Parentid  in:Hospital_id_list
        //                                 or
        //                                         Account.Parentid  in:Hospital_id_list
        //                                 or
        //                                         Account.id  in:Hospital_id_list
        //                                     )
        //                         ];
        system.debug('========1212121212'+FirstNameList);
        system.debug('========1212121212'+LastnameList);
        //system.debug('========1212121212'+LastnameEncryptedList);
        system.debug('========1212121212'+Hospital_id_list);
        if(CntingList.size()>0){
            system.debug('========2222222'+CntingList.size());
            Contact Cnting = CntingList[0];
            for(Contact a : Trigger.new) {
                system.debug('========3333333');
                if(mapCon.containsKey(a.firstname+a.Lastname)){
                system.debug('========333333');
                if( Cnting.Account.Parent.Parent.Name   !=  null    ){
                        a.addError('该客户人员名字已存在,在'+Cnting.Account.Parent.Parent.Name+'医院下属 '+Cnting.Account.Parent.Name+'战略科室的 '+Cnting.Account.Name+'科室,请修改,或者勾选“不是重复的客户名”后,再次点击保存'+Cnting);
                    }   else if (   Cnting.Account.Parent.Name  !=null  ){
                        a.addError('该客户人员名字已存在,在'+Cnting.Account.Parent.Name+'医院的 '+Cnting.Account.Name+' 战略科室下,请修改,或者勾选“不是重复的客户名”后,再次点击保存'+Cnting);
                    }   else if (   Cnting.Account.Name !=  null    ){
                        a.addError('该客户人员名字已存在,在'+Cnting.Account.Name+'医院,请修改,或者勾选“不是重复的客户名”后,再次点击保存'+Cnting);
                    }
                }
            }
        }
    //     if(CntingList.size()>0){
    //         system.debug('========2222222'+CntingList.size());
    //         Contact Cnting = CntingList[0];
    //         for(Contact a : Trigger.new) {
    //             system.debug('========3333333');
    //             if(mapCon.containsKey(a.LastName_Encrypted__c)){
    //                 //For PIPL testing by Li Jun 20220308 Start
    //                 // system.debug('========333333');
    //                 // if( Cnting.Account.Parent.Parent.Name   !=  null    ){
    //                 //         a.addError('该客户人员名字已存在,在'+Cnting.Account.Parent.Parent.Name+'医院下属 '+Cnting.Account.Parent.Name+'战略科室的 '+Cnting.Account.Name+'科室,请修改,或者勾选“不是重复的客户名”后,再次点击保存');
    //                 //     }   else if (   Cnting.Account.Parent.Name  !=null  ){
    //                 //         a.addError('该客户人员名字已存在,在'+Cnting.Account.Parent.Name+'医院的 '+Cnting.Account.Name+' 战略科室下,请修改,或者勾选“不是重复的客户名”后,再次点击保存');
    //                 //     }   else if (   Cnting.Account.Name !=  null    ){
    //                 //         a.addError('该客户人员名字已存在,在'+Cnting.Account.Name+'医院,请修改,或者勾选“不是重复的客户名”后,再次点击保存');
    //                 //     }
    //                 //For PIPL testing by Li Jun 20220308 End
    //                 }
    //         }
    //     }
    }
    //zhj MEBG新方案改造 2022-11-28 end
//***************************************INSERT 2016-12-14 By ZDF EDN*************************************
//          增加“新增客户人员入例规则”
//***************************************INSERT 2016-12-14 By ZDF EDN*************************************