global class OPDPlanWarningBatch implements Database.Batchable { String opdPlanId; Boolean flag = false; global OPDPlanWarningBatch() { } global OPDPlanWarningBatch(Boolean flag) { this.flag = flag; } global OPDPlanWarningBatch(String opdPlanId) { this.opdPlanId = opdPlanId; } global Database.QueryLocator start(Database.BatchableContext BC) { String query = //原'Select Id, Name,Ownerid,owner.alias,Account_Laboratory__r.Name from OPDPlan__c '; //新增StayOrNot__c 'Select Id, Name,Ownerid,owner.alias,StayOrNot__c ,Account_Laboratory__r.Name from OPDPlan__c '; if (flag) { //---SWAG-BMS8EJ---20200323---AddStart //请设置当担当人用户中的“在职/离职”为离职时,OPD计划为计划中的变为 取消。 query += ' where StayOrNot__c = \'已离职\' and Status__c = \'计划中\' '; //---SWAG-BMS8EJ---20200323---AddEnd } else { query += ' where ThisMonthPlan__c = 1 and Status__c = \'计划中\' '; } if (!String.isBlank(opdPlanId)) { query += ' and Id =\'' + opdPlanId + '\''; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List opdPlanList) { if (flag) { system.debug('担当人用户中的“在职/离职”为离职时,OPD计划为计划中的变为 取消'); List opdPlans = new List(); if (opdPlanList.size() > 0) { opdPlans = opdPlanList; for (OPDPlan__c opd :opdPlans) { opd.Status__c = '取消'; } update opdPlans; } } else { sendMailtoNotComplete(opdPlanList); } } global void finish(Database.BatchableContext BC) { } @TestVisible private static boolean sendMailtoNotComplete(list opdPlanList) { boolean rs = true; List sendMails = new List(); Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage(); for (OPDPlan__c OPDPlan : opdPlanList) { String title = ''; String body = ''; List toMailsList = new List(); toMailsList.add(OPDPlan.Ownerid); title = 'OPD计划' + OPDPlan.Name + '更新提醒'; body += OPDPlan.owner.alias + '您好:'; body += '
'; body += '
'; body += '您计划于本月于' + OPDPlan.Account_Laboratory__r.Name + '实行OPD计划' + OPDPlan.Name + ',请您取消或延期该计划。'; body += '' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + OPDPlan.id + '
'; body += '
'; body += '
'; body += '谢谢!'; messageNEW = new Messaging.SingleEmailMessage(); messageNEW.subject = title; //messageNEW.plainTextBody = body; messageNEW.htmlBody = body; messageNEW.setCharset('UTF-8'); messageNEW.setToAddresses(toMailsList); sendMails.add(messageNEW); } system.debug('sendMailtoNotComplete:' + sendMails); if (sendMails.size() > 0) { Messaging.SendEmailResult[] results = messaging.sendEmail(sendMails); for (Integer i = 0; i < results.size(); i++) { if (results[i].success == false) { system.debug('=====send mail error:' + results[i].errors[0].message); rs = false; } } } return rs; } }