global class RentalApplyDailyBatch implements Database.Batchable, Database.Stateful { global List errorList = new List(); global Integer allCount = 0; // 总明细条目 global Integer allCountDone = 0; global Integer updatedCount = 0; // 实际更新的条目 global Integer round = 4; // 跑几次 Boolean IsNeedExecute = false; // 2021-03-05 mzy WLIG-BYHD79 SFDC环境batch合并调查 是否符合执行条件 global RentalApplyDailyBatch() {} global RentalApplyDailyBatch(Integer rd) { round = rd; } // 2021-03-05 mzy WLIG-BYHD79 SFDC环境batch合并调查 start global RentalApplyDailyBatch(Boolean NeedExecute) { this.IsNeedExecute = NeedExecute; } // 2021-03-05 mzy WLIG-BYHD79 SFDC环境batch合并调查 end global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator([ SELECT Salesdept__c , Work_Location__c , Owner_province__c , Branch__c , Onwer_job_category__c , Salesdepartment__c , Salesdept_text_Update_F__c , Owner_Info_text_Update_F__c FROM Rental_Apply__c WHERE Salesdept_text_Update_F__c = true OR Owner_Info_text_Update_F__c = true ]); } global void execute(Database.BatchableContext BC, List raList) { allCount += raList.size(); Savepoint sp = Database.setSavePoint(); try { for(Rental_Apply__c ra:raList) { if(ra.Salesdept_text_Update_F__c) { ra.Salesdept_text__c = ra.Salesdept__c; } if(ra.Owner_Info_text_Update_F__c) { ra.Work_Location_text__c = ra.Work_Location__c; ra.Owner_province_text__c = ra.Owner_province__c; ra.Branch_text__c = ra.Branch__c; ra.Onwer_job_category_text__c = ra.Onwer_job_category__c; ra.Salesdepartment_text__c = ra.Salesdepartment__c; } } Database.update(raList); updatedCount += raList.size(); allCountDone += raList.size(); } catch (Exception e) { Database.rollback(sp); errorList.add(e.getMessage() + '\n' + e.getStackTraceString()); System.debug(LoggingLevel.ERROR, e.getMessage() + '\n' + e.getStackTraceString()); } } global void finish(Database.BatchableContext BC) { round -= 1; if (allCount != allCountDone || errorList.size() > 0) { // 出现任何错误都重跑 if (round > 0) { System.debug(round); RentalApplyDailyBatch bat = new RentalApplyDailyBatch(round); Database.executeBatch(bat); } else { BatchEmailUtil be = new BatchEmailUtil(); String[] toList = new String[]{}; String title = '申请者信息text字段更新batch失败'; String[] ccList = new String[]{}; toList.add('shuo_wang@olympus.com.cn'); for (OrgWideEmailAddress tmpEmailObj : [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE DisplayName like 'BatchNotify']) { toList.add(tmpEmailObj.Address); } be.failedMail(toList, ccList, title, String.join(this.errorList, '\n'), allCount, allCountDone, errorList.size()); be.send(); } } //2021-03-05 mzy WLIG-BYHD79 SFDC环境batch合并调查 start if(!Test.isRunningTest() &&IsNeedExecute==true){ //batch里调用下一个batch时,希望跟原有的Schedule里面传的条数保持一致 Id execBTId = Database.executebatch(new FixtureSetDetailDailyUpdateBatch(true),200); } //2021-03-05 mzy WLIG-BYHD79 SFDC环境batch合并调查 end } }