//将学会上的工作流规则 '更新会议开始前7天提醒邮件标识' 复制为batch public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable,Database.Stateful { public String query; // public String campaignId; public List campaignIds; public String logId; public Map> errorMap = new Map>(); //定义map 存储 字段值和字段名 public Map map1 = new Map(); private BatchIF_Log__c iflog; private String errorlog = ''; private String succlog = ''; private String errorIdLog = ''; public updateCampaignMailflg7BeTrueBatch() { this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public updateCampaignMailflg7BeTrueBatch(String logId){ this.query = query; this.logId = logId; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public Database.QueryLocator start(Database.BatchableContext bc) { //如果用户传入logid,则说明存在更新失败的数据 if (logId != null && logId != '') { query = 'select id,errorlog__c from BatchIF_Log__c where id = :logId'; }else{ query = 'select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status,StartDate,Mailflg_before15__c,Mailflg_after45__c,Mailflg_after3__c,BetweenTodayEndDate__c from Campaign where '; //会议开始前7,15天提醒邮件标识 服务技师和 非服务技师 query += '(Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' AND Status != \'取消\' AND Status != \'已结束(没公开)\' AND (BetweenTodayStartDate__c = 15 OR BetweenTodayStartDate__c = 7) AND (Mailflg_before15__c = false or Mailflg_before7__c = false) ) or '; //会议结束后3天提醒邮件标识 query += '( (Status = \'公开中\' or Status = \'取消申请中\' or Status = \'报告提交\') AND BetweenTodayEndDate__c = -3 AND RecordType.DeveloperName != \'5.服务培训/技师培养\' AND Mailflg_after3__c = false) OR '; //会议计划登录45天提醒邮件标识 query += '(Open_day__c = null and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开)\' AND RecordType.DeveloperName != \'5.服务培训/技师培养\' and BetweenTodayStartDate__c = -45 and Mailflg_after45__c = false) '; } return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, list scope) { List updateCamList = new List(); //说明当前查到的集合为log集合 if (logId != null && logId != '') { /*现在batch日志里的内容类似: log.ErrorLog__c = 'errorIdStart:{"Mailflg_before7__c":["ID1","ID2"],"Mailflg_before15__c":["ID3"]} 更新学会:ID1 失败,错误出现在:XXX 更新学会:ID2 失败,错误出现在:XXX 更新学会:ID3 失败,错误出现在:XXX */ //获取错误日志并转为map进行赋值 List logs = scope; BatchIF_Log__c log = logs.get(0); String jsonLogstr = log.ErrorLog__c.subString(log.ErrorLog__c.indexOf('{'),log.ErrorLog__c.indexOf('}')+1); System.debug('jsonLogstr::'+jsonLogstr); Map resultMap = (Map) JSON.deserializeUntyped(jsonLogstr); System.debug('resultMap::'+resultMap); List sobjList = new List(); for (String key : resultMap.keySet()) { String tempStr = String.valueOf(resultMap.get(key)); String str = tempStr.substring(tempStr.indexOf('(')+1,tempStr.indexOf(')')); for (String value : str.split(',')) { Sobject cam = new Campaign(); cam.put(key, true); cam.put('Id', value.trim().substring(0,15)); map1.put(value.trim().substring(0,15), key); sobjList.add(cam); } } updateCamList = (List)sobjList; }else{ Date today = Date.today(); List camList = new List(); camList = scope; //判断各个需要更新的字段 for (Campaign cam : camList) { String camId = String.valueOf(cam.Id).substring(0,15); //记录类型是 5.服务培训/技师培养 时,更新更新服务技师培训项目开始前7天提醒邮件标识为true if (cam.RecordType.DeveloperName == 'ServiceEngineerTraining') { if (cam.Open_day__c != null && cam.Status != '已提交报告' && cam.Status != '已结束' && cam.Status != '取消' && cam.Status != '已结束(没公开)' && cam.StartDate != null && today.daysBetween(cam.StartDate) == 7 && cam.Mailflg_before7_service__c == false) { cam.Mailflg_before7_service__c = true; updateCamList.add(cam); map1.put(camId, 'Mailflg_before7_service__c'); } }else{ if (cam.Open_day__c != null && cam.Status != '已提交报告' && cam.Status != '已结束' && cam.Status != '取消' && cam.Status != '已结束(没公开)' ) { //会议开始前15天提醒邮件标识 if (cam.Mailflg_before15__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 15) { cam.Mailflg_before15__c = true; map1.put(camId, 'Mailflg_before15__c'); updateCamList.add(cam); } //会议开始前7天提醒邮件标识 if (cam.Mailflg_before7__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 7) { cam.Mailflg_before7__c = true; map1.put(camId, 'Mailflg_before7__c'); updateCamList.add(cam); } } //会议计划登录45天提醒邮件标识 if (cam.Status != '已结束' && cam.Status != '取消' && cam.Status!= '已结束(没公开)' && cam.Open_day__c == null && cam.Mailflg_after45__c == false ) { cam.Mailflg_after45__c = true; map1.put(camId, 'Mailflg_after45__c'); updateCamList.add(cam); } //会议结束后3天提醒邮件标识 if ((cam.Status== '公开中' || cam.Status == '取消申请中' || cam.Status == '报告提交' ) && cam.Mailflg_after3__c == false) { cam.Mailflg_after3__c = true; map1.put(camId, 'Mailflg_after3__c'); updateCamList.add(cam); } } } } Database.SaveResult[] lsr = Database.update(updateCamList, false); for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) { Database.SaveResult sr = lsr[tIdx]; //如果更新失败 if (!sr.isSuccess()) { Database.Error emsg = sr.getErrors()[0]; String errorId = updateCamList[tIdx].Id; String errorColumn = map1.get(errorId.substring(0,15)); if (!errorMap.containsKey(errorColumn)) { errorMap.put(errorColumn, new List()); } errorMap.get(errorColumn).add(errorId); errorlog += '更新学会:'+errorId +'失败,错误出现在:'+emsg.getMessage() +'\n'; }else{ succlog += '更新学会:'+updateCamList[tIdx].Id +'成功\n'; } } } public void finish(Database.BatchableContext BC) { if (!errorMap.containsKey(null) && errorMap != null && errorMap.size() > 0) { //转json 方便传id时转换 iflog.ErrorLog__c = 'errorIdStart:\n'+JSON.serialize(errorMap)+'\n'; } iflog.ErrorLog__c += errorlog; iflog.Log__c += succlog; iflog.Log__c += 'end --'; insert iflog; } } /*//将学会上的工作流规则 '更新会议开始前7天提醒邮件标识' 复制为batch public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable,Database.Stateful { public String query; // public String campaignId; public List campaignIds; public String logId; private BatchIF_Log__c iflog; private String errorlog = ''; private String succlog = ''; private String errorIdLog = ''; public updateCampaignMailflg7BeTrueBatch() { this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } // public updateCampaignMailflg7BeTrueBatch(List camIds) { // this.query = query; // this.campaignIds = camIds; // iflog = new BatchIF_Log__c(); // iflog.Type__c = 'updateCampaignMailflg7BeTrue'; // iflog.Log__c = 'start --\n'; // iflog.ErrorLog__c = ''; // } public updateCampaignMailflg7BeTrueBatch(String logId){ this.query = query; this.logId = logId; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public Database.QueryLocator start(Database.BatchableContext bc) { //如果用户传入logid,则说明存在更新失败的数据 if (logId != null && logId != '') { query = 'select id,errorlog__c from BatchIF_Log__c where id = :logId'; }else{ query = 'select id,Mailflg_before7__c,Name from Campaign where Open_day__c != null '; query += ' and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\''; query += ' and BetweenTodayStartDate__c = 7 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before7__c = false '; // if (campaignIds != null && campaignIds.size() > 0) { // query += ' and id in :campaignId '; // } } return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, list scope) { List updateCamList = new List(); //说明当前查到的集合为log集合 if (logId != null && logId != '') { //截取字符串,获取更新失败的学会id和学会的字段 List logs = scope; BatchIF_Log__c log = logs.get(0); String logStr = log.ErrorLog__c.substring(log.ErrorLog__c.indexOf(':')+1,log.ErrorLog__c.indexOf('失败')); System.debug('123333::::logstr:'+logStr); String[] errIds = logStr.split(','); for (String id : errIds) { Campaign cam = new Campaign(); cam.Id = id; cam.Mailflg_before7__c = true; updateCamList.add(cam); } }else{ updateCamList = scope; for (Campaign cam : updateCamList) { cam.Mailflg_before7__c = true; } System.debug('20------'+updateCamList); } Database.SaveResult[] lsr = Database.update(updateCamList, false); for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) { Database.SaveResult sr = lsr[tIdx]; //如果更新失败 if (!sr.isSuccess()) { Database.Error emsg = sr.getErrors()[0]; errorIdLog += updateCamList[tIdx].Id+','; errorlog += '更新学会:'+updateCamList[tIdx].Id +'失败,错误出现在:'+emsg.getMessage() +'\n'; }else{ succlog += '更新学会:'+updateCamList[tIdx].Id +'成功\n'; } } // for (Campaign cam : scope) { // cam.Mailflg_before7__c = true; // } // System.debug('20------'+scope); // Database.SaveResult[] lsr = Database.update(scope, false); // for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) { // Database.SaveResult sr = lsr[tIdx]; // //如果更新失败 // if (!sr.isSuccess()) { // Database.Error emsg = sr.getErrors()[0]; // errorIdLog += scope[tIdx].Id+','; // errorlog += '更新学会:'+scope[tIdx].Name +'失败,错误出现在:'+emsg.getMessage() +'\n'; // }else{ // succlog += '更新学会:'+scope[tIdx].Name +'成功\n'; // } // } System.debug('55::'+updateCamList); // update scope; } public void finish(Database.BatchableContext BC) { System.debug('60001111:::'); if (errorIdLog.length() > 0) { iflog.ErrorLog__c += '更新:'+errorIdLog.substring(0,errorIdLog.length()-1) +'失败\n'; } iflog.ErrorLog__c += errorlog; iflog.Log__c += succlog; iflog.Log__c += 'end --'; insert iflog; } } */ /*//将学会上的工作流规则 '更新会议开始前7天提醒邮件标识' 复制为batch public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable,Database.Stateful { public String query; public String campaignId; private BatchIF_Log__c iflog; private String errorlog = ''; private String succlog = ''; private String errorIdLog = ''; public updateCampaignMailflg7BeTrueBatch() { this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public updateCampaignMailflg7BeTrueBatch(String camId) { this.query = query; this.campaignId = camId; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public Database.QueryLocator start(Database.BatchableContext bc) { query = 'select id,Mailflg_before7__c,Name from Campaign where Open_day__c != null '; query += ' and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\''; query += ' and BetweenTodayStartDate__c = 7 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before7__c = false '; if (campaignId != null && campaignId != '') { query += ' and id = :campaignId '; } return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, list scope) { for (Campaign cam : scope) { cam.Mailflg_before7__c = true; } System.debug('20------'+scope); Database.SaveResult[] lsr = Database.update(scope, false); for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) { Database.SaveResult sr = lsr[tIdx]; //如果更新失败 if (!sr.isSuccess()) { Database.Error emsg = sr.getErrors()[0]; errorIdLog += 'Id:'+scope[tIdx].Id+'\n'; errorlog += '更新学会:'+scope[tIdx].Name +'失败,错误出现在:'+emsg.getMessage() +'\n'; }else{ succlog += '更新学会:'+scope[tIdx].Name +'成功\n'; } } System.debug('55::'+iflog); // update scope; } public void finish(Database.BatchableContext BC) { System.debug('60001111:::'); iflog.ErrorLog__c += '更新:\n'; iflog.ErrorLog__c += errorIdLog +'失败'; iflog.ErrorLog__c += errorlog; iflog.Log__c += succlog; iflog.Log__c += 'end --'; insert iflog; } }*/ /*//将学会上基于时间的更新赋值复制到batch中 public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable,Database.Stateful { public String query; public String campaignId; private BatchIF_Log__c iflog; private String errorlog = ''; private String succlog = ''; public updateCampaignMailflg7BeTrueBatch() { this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public updateCampaignMailflg7BeTrueBatch(String camId) { this.query = query; this.campaignId = camId; iflog = new BatchIF_Log__c(); iflog.Type__c = 'updateCampaignMailflg7BeTrue'; iflog.Log__c = 'start --\n'; iflog.ErrorLog__c = ''; } public Database.QueryLocator start(Database.BatchableContext bc) { if (campaignId != null && campaignId != '') { return Database.getQueryLocator( [select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status from Campaign where id = :campaignId and (Mailflg_before15__c = false or Mailflg_cancel__c = false or Mailflg_after45__c = false or Mailflg_after3__c = false or Mailflg_before7_service__c = false or Mailflg_before7__c = false)] ); }else{ return Database.getQueryLocator([ select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status from Campaign where Mailflg_before15__c = false or Mailflg_cancel__c = false or Mailflg_after45__c = false or Mailflg_after3__c = false or Mailflg_before7_service__c = false or Mailflg_before7__c = false ]); } // return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, list scope) { Date today = Date.today(); //查到的学会就是需要更新相关字段为true的学会,需要判断把谁变成true for (Campaign cam : scope) { //记录类型是 5.服务培训/技师培养 时,更新更新服务技师培训项目开始前7天提醒邮件标识为true if (cam.RecordType.DeveloperName == '5.服务培训/技师培养') { cam.Mailflg_before7_service__c = true; succlog += '72:--'; }else{ //会议计划取消提醒邮件标识 if (cam.Status== '取消' && cam.Open_day__c != null ) { cam.Mailflg_cancel__c = true; succlog += '77:--'; } if (cam.Open_day__c != null && cam.Status != '已提交报告' && cam.Status != '已结束' && cam.Status != '取消' && cam.Status != '已结束(没公开)' ) { //会议开始前15天提醒邮件标识 if (cam.Mailflg_before15__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 15) { cam.Mailflg_before15__c = true; succlog += '83:--'; } //会议开始前7天提醒邮件标识 if (cam.Mailflg_before15__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 7) { cam.Mailflg_before7__c = true; succlog += '88:--'; } } //会议计划登录45天提醒邮件标识 if (cam.Status != '已结束' && cam.Status != '取消' && cam.Status!= '已结束(没公开)' && cam.Open_day__c == null) { cam.Mailflg_after45__c = true; succlog += '95:--'; } //会议结束后3天提醒邮件标识 if ((cam.Status== '公开中' || cam.Status == '取消申请中' || cam.Status == '报告提交' ) && cam.BetweenTodayEndDate__c >= -3) { cam.Mailflg_after3__c = true; succlog += '94:--'; } } // cam.Mailflg_before7__c = true; // cam.HostName__c = null; } System.debug('10111::'+scope); System.debug('20------'+scope); Database.SaveResult[] lsr = Database.update(scope, false); for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) { Database.SaveResult sr = lsr[tIdx]; //如果更新失败 if (!sr.isSuccess()) { Database.Error emsg = sr.getErrors()[0]; errorlog += '更新学会:'+scope[tIdx].Id +scope[tIdx].Name +'失败,错误出现在:'+emsg.getMessage() +'\n'; }else{ succlog += '更新学会:'+scope[tIdx].Name +'成功\n'; } } System.debug('55::'+iflog); // update scope; } public void finish(Database.BatchableContext BC) { System.debug('60001111:::'); iflog.ErrorLog__c += errorlog; iflog.Log__c += succlog; iflog.Log__c += 'end --'; insert iflog; } } */ /* // query = 'select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status from Campaign where Mailflg_before15__c = false or Mailflg_cancel__c = false or Mailflg_after45__c = false or Mailflg_after3__c = false or Mailflg_before7_service__c = false or Mailflg_before7__c = false '; // if (campaignId != null && campaignId != '') { // query += 'and id = :campaignId'; // } //会议开始前15天提醒邮件标识 // query += ' (Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and BetweenTodayStartDate__c >= 15 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before15__c = false ) '; // //会议计划取消提醒邮件标识 // query += ' or (Status = \'取消\' and Open_day__c != null and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_cancel__c = false) '; // //会议计划登录45天提醒邮件标识 // query += '( Open_day__c = null and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_after45__c = false) OR'; // //会议结束后3天提醒邮件标识 // query += '( (Status = \'公开中\' or Status = \'取消申请中\' or Status = \'报告提交\') and BetweenTodayEndDate__c >= -3 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_after3__c = false ) OR'; // //更新服务技师培训项目开始前7天提醒邮件标识 // query += '(Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and BetweenTodayStartDate__c >= 7 and RecordType.DeveloperName = \'5.服务培训/技师培养\' and Mailflg_before7_service__c = false ) or'; // //会议开始前7天提醒邮件标识 // query += '( Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and BetweenTodayStartDate__c = 7 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before7__c = false )'; */