global class AccAssumeChangeBatch implements Database.Batchable , Database.AllowsCallouts, Database.Stateful { public List HospitalIdList;//执行失败手动录入医院 Boolean a = true; //执行失败手动录入医院 global AccAssumeChangeBatch(List HospitalIdList) { this.HospitalIdList = HospitalIdList; } global AccAssumeChangeBatch( boolean a) { this.a = a; } // 执行靶器 global AccAssumeChangeBatch() { } global Database.QueryLocator start(Database.BatchableContext bc) { // 检索医院(用来对应其关联的招投标) String query = 'Select Id, Assume_Change__c FROM Account '; if (a) { query += 'Where Assume_Change__c = true'; } if (HospitalIdList.size() > 0) { query += ' And Id in : HospitalIdList'; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list HosinfoList) { List TenInfoList = [Select Id, Hospital__c, Hospital1__c, Hospital2__c, Hospital3__c, Hospital4__c from Tender_information__c Where Hospital__c in: HosinfoList or Hospital1__c in: HosinfoList or Hospital2__c in: HosinfoList or Hospital3__c in: HosinfoList or Hospital4__c in: HosinfoList ]; if (TenInfoList.size() > 0) { // List failedTenderList = new List(); Map failedAccountMap = new Map();//如果招投标更新失败,用来存更新失败的招投标所关联的医院 // 空更新招投标 Database.SaveResult[] saveTenderResults = Database.update(TenInfoList, false); // 循环更新结果,取出空更新失败的招投标所对应的医院 for (Integer i = 0; i < saveTenderResults.size(); i++) { if (!saveTenderResults.get(i).isSuccess() || System.Test.isRunningTest()) { // failedTenderList.add(TenInfoList.get(i)); String HospitalId = TenInfoList.get(i).Hospital__c; String Hospital1Id = TenInfoList.get(i).Hospital1__c; String Hospital2Id = TenInfoList.get(i).Hospital2__c; String Hospital3Id = TenInfoList.get(i).Hospital3__c; String Hospital4Id = TenInfoList.get(i).Hospital4__c; failedAccountMap.put(HospitalId, HospitalId); failedAccountMap.put(Hospital1Id, Hospital1Id); failedAccountMap.put(Hospital2Id, Hospital2Id); failedAccountMap.put(Hospital3Id, Hospital3Id); failedAccountMap.put(Hospital4Id, Hospital4Id); } } List accsList = new List();// 用来更新客户信息(担当发生变化) for (Account accs : HosinfoList) { if (!failedAccountMap.containsKey(accs.Id) ) { accs.Assume_Change__c = false; accsList.add(accs); } } if (accsList.size() > 0) { update accsList; } } } global void finish(Database.BatchableContext BC) { } }