public without sharing class AgencyContactHandler extends Oly_TriggerHandler { @TestVisible private Map newMap; @TestVisible private Map oldMap; @TestVisible private List newList; @TestVisible private List oldList; public AgencyContactHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert() { beforeSetValue(); } protected override void beforeUpdate() { beforeSetValue(); shareAgency_Contact_ToRole(this.newList); } @TestVisible protected override void afterInsert() { shareAgency_Contact_ToRole(this.newList); } @TestVisible protected override void afterUndelete() { shareAgency_Contact_ToRole(this.newList); } private void beforeSetValue() { Set nameSet = new Set(); Set ahIdSet = new Set(); Map keyMap = new Map(); for (Agency_Contact__c nObj : newList) { if (String.isBlank(nObj.Agency_ID__c)) { if (String.isBlank(nObj.getCloneSourceId()) == false) { // 要注意 画面からしかないので、ここで selectする Agency_Contact__c srcObj = [Select Id, Agency_Hospital__c, Contact__c, Hospital_ID18__c, Hospital_Name__c from Agency_Contact__c where Id = :nObj.getCloneSourceId()]; if (String.isBlank(nObj.Agency_Hospital__c) == false && String.isBlank(srcObj.Agency_Hospital__c) && nObj.Hospital_ID18__c != srcObj.Hospital_ID18__c) { nObj.Agency_Hospital__c.addError('请选择:' + srcObj.Hospital_Name__c); } if (String.isBlank(srcObj.Contact__c) == false) { nObj.Contact__c = srcObj.Contact__c; } } nObj.Agency_ID__c = nObj.Agency_ID_F__c; } nameSet.add(nObj.Name_Encrypted__c); ahIdSet.add(nObj.Agency_Hospital__c); // PIPL update Yin Mingjie 21/02/2022 start // keyMap.put(nObj.Agency_Hospital__c + nObj.Name, nObj); keyMap.put(nObj.Agency_Hospital__c + nObj.Name_Encrypted__c, nObj); // PIPL update Yin Mingjie 21/02/2022 end } // PIPL update Yin Mingjie 21/02/2022 start // List ars = [select Id, Agency_Hospital__r.Name, Agency_Hospital__c, Name from Agency_Contact__c // where Name in :nameSet and Agency_Hospital__c in :ahIdSet and Agency_Hospital__c != null]; List ars = [select Id, Agency_Hospital__r.Name, Agency_Hospital__c, Name, Name_Encrypted__c from Agency_Contact__c where Name_Encrypted__c in :nameSet and Agency_Hospital__c in :ahIdSet and Agency_Hospital__c != null]; // PIPL update Yin Mingjie 21/02/2022 end for (Agency_Contact__c ar : ars) { // PIPL update Yin Mingjie 21/02/2022 start // String key = ar.Agency_Hospital__c + ar.Name; String key = ar.Agency_Hospital__c + ar.Name_Encrypted__c; // PIPL update Yin Mingjie 21/02/2022 end //system.debug('========3333333key'+key); if (keyMap.containsKey(key)) { Agency_Contact__c a = keyMap.get(key); if (a.Id == ar.Id) continue; //system.debug('========3333333'); a.addError('该客户人员名字已存在,在'+ar.Agency_Hospital__r.Name+'医院,请修改'); } } } /** * 设定 apex share to role * @param accId UserRoleとGroupを取得するため */ private static void setAgency_Contact_Share(Id accId, List pList) { Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(accId); if(grpId != null||Test.isRunningTest()) { List shareList = new List(); Set sharePIdSet = new Set(); for (Agency_Contact__Share share : [SELECT Id, ParentId FROM Agency_Contact__Share WHERE UserOrGroupId = :grpId AND RowCause = 'Manual']) { sharePIdSet.add(share.ParentId); } for (Id pId : pList) { if (sharePIdSet.contains(pId) == false) { Agency_Contact__Share apexShare = new Agency_Contact__Share( RowCause = 'Manual', ParentId = pId, UserOrGroupId = grpId, AccessLevel = 'Edit'); shareList.add(apexShare); } } if (System.Test.isRunningTest() && grpId == null) { //没有造 User 的旧Test 也让可以过 return; } insert shareList; } } // afterInsert, afterUndelete /** * 一定要是同一经销商的 * @param nList {Id, Agency_ID__c} */ public static void shareAgency_Contact_ToRole(List nList) { Set targetAgencyIdSet = new Set(); Map> tMap = new Map>(); for (Agency_Contact__c nObj : nList) { if (nObj.Agency_ID__c == '000000000000000') continue; targetAgencyIdSet.add(nObj.Agency_ID__c); List tList = tMap.get(nObj.Agency_ID__c); if (tList == null) { tList = new List(); } tList.add(nObj.Id); tMap.put(nObj.Agency_ID__c, tList); } if (targetAgencyIdSet.size() > 0) { List targetIdList = new List (targetAgencyIdSet); System.assertEquals(1, targetIdList.size(), '一定要是同一经销商 [' + targetIdList[0] + ']'); setAgency_Contact_Share(targetIdList[0], tMap.get(targetIdList[0])); } } }