| //医療従事者の診療科(兼務先)を変更の場合、戦略科室・病院も修正 | 
| 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 | 
|                     && (Trigger.oldMap.get(a.Id).get('AccountId') != a.AccountId | 
|                         || a.AccountParentId__c != a.Strategic_dept_Class__c))) { | 
|             if (!String.isBlank(a.AccountId)) { | 
|                 accIds.add(a.AccountId); | 
|             } | 
|         } | 
|         if(Trigger.isInsert&&a.Ignore_Same_Name__c==false){ | 
|             FirstNameList.add(a.firstname); | 
|             LastnameList.add(a.Lastname); | 
|             LastnameEncryptedList.add(a.LastName_Encrypted__c); | 
|             ACCOUNT_id_list.add(a.Accountid); | 
|             mapCon.put(a.LastName_Encrypted__c,a); | 
|             system.debug('========1111111'); | 
|         } | 
|   | 
|     } | 
|   | 
|     system.debug('========FirstNameList'+FirstNameList+'###'+FirstNameList.size()); | 
|     if(FirstNameList.size()==1){ | 
|         List<Account> AccountList   =   [   select | 
|                                                     id, | 
|                                                     ParentId, | 
|                                                     Parent.ParentId, | 
|                                                     Name | 
|                                             from | 
|                                                     Account | 
|                                             where | 
|                                                     id  in:ACCOUNT_id_list | 
|                                         ]; | 
|         for(    Account acc :   AccountList ){ | 
|             if( acc.Parent.ParentId !=  null    ){ | 
|                 Hospital_id_list.add(   acc.Parent.ParentId ); | 
|             }else   if( acc.ParentId    !=  null    ){ | 
|                 Hospital_id_list.add(   acc.ParentId    ); | 
|             }else { | 
|                 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 | 
|                                         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.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 | 
|                     } | 
|             }    | 
|         } | 
|     } | 
| //***************************************INSERT 2016-12-14 By ZDF EDN************************************* | 
| //          增加“新增客户人员入例规则” | 
| //***************************************INSERT 2016-12-14 By ZDF EDN************************************* | 
|   | 
|   | 
|     if (accIds.size() > 0) { | 
|         // 診療科レコードタイプ | 
|         String[] deptTypes = new String[] {'診療科 その他', '診療科 呼吸科', '診療科 婦人科', '診療科 普外科', '診療科 泌尿科', '診療科 消化科', '診療科 耳鼻喉科'}; | 
|         List<RecordType> deptRects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :deptTypes]; | 
|         Map<String, String> deptRectMap = new Map<String, String>(); | 
|         for (RecordType rect : deptRects) { | 
|             deptRectMap.put(rect.Id, rect.Name); | 
|         } | 
|          | 
|         Map<Id, Account> accMap = new Map<Id, Account>(); | 
|         List<Account> accs = ControllerUtil.selectAccountForTrigger(accIds); | 
|         for(Account acc : accs) { | 
|             accMap.put(acc.Id, acc); | 
|         } | 
|         for(Contact a : Trigger.new) { | 
|             // 「診療科」に診療科を選択する場合 | 
|             if (accMap.get(a.AccountId) != null | 
|                 && deptRectMap.get(accMap.get(a.AccountId).RecordTypeId) != null) { | 
|                 Account acc = accMap.get(a.AccountId); | 
|                 a.Strategic_dept_Class__c = acc.ParentId; | 
|             } | 
|         } | 
|     } | 
| } |