public without sharing class RequestTednerDocHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; private List qisIdList; public RequestTednerDocHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void afterInsert() { updateTask(); } protected override void afterUpdate() { updateTask(); } private void updateTask() { List requestIdList = new List(); // Map requestMap = new Map(); for (Request_tedner_doc__c rtd : newList) { if ((Trigger.isInsert || oldMap.get(rtd.Id).get('Status__c') != rtd.Status__c)) { // 授权申请ID RequestIdList.add(rtd.Id); } } if (RequestIdList != null && RequestIdList.size() > 0) { String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); List updTask = new List(); List delTask = new List(); Map tskMap = new Map(); List lstTask = [ Select Id, Subject, OwnerId, Description, ActivityDate, RequestOpp__c From Task Where RequestOpp__c In :RequestIdList limit 10 ]; for(Task tsk : lstTask) { tskMap.put(String.valueOf(tsk.RequestOpp__c) + tsk.OwnerId, tsk); } for (Request_tedner_doc__c rtd : newList) { if ((Trigger.isInsert || oldMap.get(rtd.Id).get('Status__c') != rtd.Status__c) && rtd.Status__c == '已提交' ) { // 提交委托的时候,更新或新增任务 Task tsk = new Task(); String key = rtd.Id + String.valueof(rtd.Request_target__c); if (tskMap.containsKey(key)) { tsk = tskMap.get(key); } String todoSubject = '授权委托:' + rtd.Prospect_number__c; String description = rtd.Tender_request_reason__c; tsk.OwnerId = rtd.Request_target__c; tsk.Subject = todoSubject; tsk.Description = description; tsk.ActivityDate = Date.today(); tsk.RequestOpp__c = rtd.Id; tsk.ReportUrlText__c = baseUrl + '/' + rtd.Id; updTask.add(tsk); } else if(Trigger.isUpdate && oldMap.get(rtd.Id).get('Status__c') == '已提交' && rtd.Status__c == '草案中' ){ // 取消委托的时候,将任务删除 String key = rtd.Id + String.valueof(rtd.Request_target__c); if (tskMap.containsKey(key)) { delTask.add(tskMap.get(key)); } } } if (updTask.size() > 0) { upsert updTask; } if (delTask.size() > 0) { delete delTask; } } } }