public without sharing class CampaignCostHandler extends Oly_TriggerHandler { @TestVisible private Map newMap; @TestVisible private Map oldMap; @TestVisible private List newList; @TestVisible private List oldList; private id HuaBeiCostID; public CampaignCostHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; Group HuaBeiCost = [SELECT Id FROM Group WHERE DeveloperName = 'X00_Meetingfee_Huabei']; HuaBeiCostID = HuaBeiCost.id; } protected override void afterInsert() { ShareWork(); } protected override void afterUpdate() { ShareWork(); } protected override void afterUndelete() { ShareWork(); } @TestVisible private void ShareWork() { list updateCamCostList = new list(); list updateCamCostIdList = new list(); for (Campaign_Cost__c tempCamCost : newList) { if (checkUpdateShare(tempCamCost)) { updateCamCostList.add(tempCamCost); updateCamCostIdList.add(tempCamCost.id); } } if (updateCamCostIdList.size() > 0) { UpdateShare(updateCamCostList, updateCamCostIdList); } } // 通过是否新建、取消删除或经理部长总监换人,担当所属部门换到1、华北或者从1、华为换为别的 // 判断是否需要更新这条数据共享规则 @TestVisible private boolean checkUpdateShare(Campaign_Cost__c tempCamCost) { if (Trigger.isInsert || Trigger.isUndelete) { return true; } else if ( tempCamCost.Sponsor_Ones_Post__c != oldMap.get(tempCamCost.id).Sponsor_Ones_Post__c) { return true; } else if ( tempCamCost.JingliApprovalManager__c != tempCamCost.OwnerId && tempCamCost.JingliApprovalManager__c != oldMap.get(tempCamCost.id).JingliApprovalManager__c) { return true; } else if ( tempCamCost.BuchangApprovalManager__c != tempCamCost.OwnerId && tempCamCost.BuchangApprovalManager__c != oldMap.get(tempCamCost.id).BuchangApprovalManager__c) { return true; } else if ( tempCamCost.ZongjianApprovalManager__c != tempCamCost.OwnerId && tempCamCost.ZongjianApprovalManager__c != oldMap.get(tempCamCost.id).ZongjianApprovalManager__c) { return true; } return false; } // 更新共享规则 @TestVisible private void UpdateShare(list updateCamCostList, list updateCamCostIdList) { list deleteCamCostShareList = [SELECT Id, ParentId, UserOrGroupId, AccessLevel FROM Campaign_Cost__Share where ParentId in : updateCamCostIdList and AccessLevel = 'Read' ]; if (deleteCamCostShareList.size() > 0) { delete deleteCamCostShareList; } list InsertCamCostShareList = new list(); for (Campaign_Cost__c tempCamCost : updateCamCostList) { List CamCostShareList = SetUpCamCostShareList(tempCamCost); if (CamCostShareList.size() > 0) { InsertCamCostShareList.addAll(CamCostShareList); } } if (InsertCamCostShareList.size() > 0) { insert InsertCamCostShareList; } } @TestVisible private List SetUpCamCostShareList(Campaign_Cost__c tempCamCost) { List CamCostShareList = new List(); if (tempCamCost.JingliApprovalManager__c != null && tempCamCost.JingliApprovalManager__c != tempCamCost.OwnerId ) { Campaign_Cost__Share temCamCostShare = new Campaign_Cost__Share(ParentId = tempCamCost.id, UserOrGroupId = tempCamCost.JingliApprovalManager__c, AccessLevel = 'Read'); CamCostShareList.add(temCamCostShare); } if (tempCamCost.BuchangApprovalManager__c != null && tempCamCost.BuchangApprovalManager__c != tempCamCost.OwnerId ) { Campaign_Cost__Share temCamCostShare = new Campaign_Cost__Share(ParentId = tempCamCost.id, UserOrGroupId = tempCamCost.BuchangApprovalManager__c, AccessLevel = 'Read'); CamCostShareList.add(temCamCostShare); } if (tempCamCost.ZongjianApprovalManager__c != null && tempCamCost.ZongjianApprovalManager__c != tempCamCost.OwnerId) { Campaign_Cost__Share temCamCostShare = new Campaign_Cost__Share(ParentId = tempCamCost.id, UserOrGroupId = tempCamCost.ZongjianApprovalManager__c, AccessLevel = 'Read'); CamCostShareList.add(temCamCostShare); } if (tempCamCost.Sponsor_Ones_Post__c != null && tempCamCost.Sponsor_Ones_Post__c.equals('1.华北')) { Campaign_Cost__Share temCamCostShare = new Campaign_Cost__Share(ParentId = tempCamCost.id, UserOrGroupId = HuaBeiCostID, AccessLevel = 'Read'); CamCostShareList.add(temCamCostShare); } return CamCostShareList; } }