// 更新联系人上的开展状态 和 带教时长(月) global class UpdateMeetingToContactBatch implements Database.Batchable { list idlist; Boolean IsNeedExecute = false; // 2021-03-03 mzy WLIG-BYHD79 SFDC环境batch合并调查 是否符合执行条件 global UpdateMeetingToContactBatch() { idList = null; } global UpdateMeetingToContactBatch(List idlist) { this.idList = idlist; } global UpdateMeetingToContactBatch(Boolean needExecute) { idList = null; IsNeedExecute = needExecute; // 2021-03-03 mzy WLIG-BYHD79 SFDC环境batch合并调查 } global Database.QueryLocator start(Database.BatchableContext BC) { string query = ''; query = 'select id from contact where Campaign__c != null'; if (idList != null && idList.size() > 0) { query += ' and id in: idList'; } system.debug('query:' + query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List contactlist) { Date tempToday = Date.today(); Date sixMonthAgo = tempToday.addMonths(-6); list allcontactID = new list(); for (contact tempContact : contactlist) { allcontactID.add(tempContact.id); } // 开展状态为开展中的客户人员汇集 set ongoingContactSet = new set(); list MeetingManagementList = [select id, Contact__c from MeetingManagement__c where CreatedDate__c >= : sixMonthAgo and Contact__c in: contactlist and ( (pollingTime__c !=0 and pollingTime__c != null) or (VisitTime__c !=0 and VisitTime__c != null) or (InspectTime__c !=0 and InspectTime__c != null) or (InspectEquipmentTime__c !=0 and InspectEquipmentTime__c != null) or (TeachingTime__c !=0 and TeachingTime__c != null) or (MaintenanceReportTime__c !=0 and MaintenanceReportTime__c != null) ) ]; for (MeetingManagement__c temMeetingManagement : MeetingManagementList) { ongoingContactSet.add(temMeetingManagement.Contact__c); } // 带教时长(月)计算 map ContactInstructTime = new map(); List InstructedStaffList = [select count(id) Cnt_Id, ContactID__c from Instructed_staff__c where ContactID__c in: allcontactID and Instruct_report__r.Status__c = '批准' group by ContactID__c]; for (AggregateResult temAgg : InstructedStaffList) { ContactInstructTime.put((ID) temAgg.get('ContactID__c'), integer.valueof(temAgg.get('Cnt_Id'))); } list UpdateContactList = new list(); for (ID contactID : allcontactID) { Contact temContact = new Contact(id = contactID); if (ongoingContactSet.Contains(contactID)) { temContact.ProcessingWorkStatus__c = '开展中'; } else { temContact.ProcessingWorkStatus__c = '未开展'; } if (ContactInstructTime.containskey(contactID)) { temContact.teachMonth__c = ContactInstructTime.get(contactID); } else { temContact.teachMonth__c = 0; } UpdateContactList.add(temContact); } if (UpdateContactList.size() > 0) { update UpdateContactList; } } global void finish(Database.BatchableContext BC) { //2021-03-03 mzy WLIG-BYHD79 SFDC环境batch合并调查 start if(!Test.isRunningTest() &&IsNeedExecute==true){ //因Batch中构造方法已有单个Boolean参数,因此创建了两个参数的构造方法 //batch里调用下一个batch时,希望跟原有的Schedule里面传的条数保持一致 Id execBTId = Database.executebatch(new ContactInstructCountNSetNullBatch(true,true),100); } //2021-03-03 mzy WLIG-BYHD79 SFDC环境batch合并调查 end } }