public without sharing class InquiryFormHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public InquiryFormHandler(){ this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert() { beforeExecute(); } protected override void beforeUpdate() { beforeExecute(); } protected override void afterInsert() { shareToOSCM(); } protected override void afterUpdate() { shareToOSCM(); } //Before処理 private void beforeExecute() { Map accMap = new Map(); for(Inquiry_form__c nObj : newList) { List accList = [Select Id,OwnerId from Account where Id =: nObj.Department_ID__c]; for(Account acc : accList){ nObj.Depart_Owner__c = acc.OwnerId; } } } // 战略科室的主担当 を取得、Inquiry_form__Shareに設定(Read) private void shareToOSCM() { //存放用于新增的共享数据 List insertList = new List(); //存放(会议询问单id,共享对象) Map insertMap = new Map(); //存放最后需要新增的共享数据 List lastInsertList = new List(); //存放已有的相同的共享原因的数据 List deleteTargetAOIdList = new List(); //存放用于的id List userIdList = new List(); // String rowCause = 'Manual'; //新增一个共享原因 String rowCause = Schema.Inquiry_form__Share.RowCause.OCSM_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; System.debug('战略科室担当:'+nObj.Depart_Owner__c); if (oldMap != null && oldMap.containsKey(nObj.Id)) { oObj = oldMap.get(nObj.Id); } if ( nObj.Depart_Owner__c != null && (oObj == null || oObj.Depart_Owner__c != nObj.Depart_Owner__c)) { Inquiry_form__Share aos = new Inquiry_form__Share( RowCause = rowCause, ParentId = nObj.Id, UserOrGroupId = nObj.Depart_Owner__c, AccessLevel = 'Edit'); //存放要新增的共享数据 insertList.add(aos); System.debug('key:'+nObj.Id); //存放(会议询问单id,共享对象); insertMap.put(nObj.Id,aos); //存放用户id 用作检索条件 userIdList.add(nObj.Depart_Owner__c); if (oObj != null && oObj.Depart_Owner__c != nObj.Depart_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 deleteList = [SELECT Id FROM Inquiry_form__Share WHERE RowCause = :rowCause AND ParentId IN :deleteTargetAOIdList ]; delete deleteList; } //判断需要共享的人 是不是创建人 如果是则说明有一条这个人的owner数据 则不新增 if (insertMap!= null) { List 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; } } }