global class RepairResonCannotWarrantyHistoricalBatch implements Database.Batchable { public String query; global RepairResonCannotWarrantyHistoricalBatch() { this.query = query; } global Database.QueryLocator start(Database.BatchableContext bc) { query='Select Delivered_Product__r.id,Delivered_Product__r.Reson_Can_not_Warranty__c,CancellationReason__c ' +' from Repair__c where Return_Without_Repair_IF__c = true and Return_Without_Repair_Reason__c = \'I/F理由、確認\' and( CancellationReason__c =\'报价单到期\' or CancellationReason__c =\'废弃\' or CancellationReason__c =\'购买新品\' or CancellationReason__c =\'零件停产\' or CancellationReason__c =\'零件已停止生产\' ' +' or CancellationReason__c =\'到其他公司修理\' or CancellationReason__c =\'其他\' or CancellationReason__c =\'修理价格高\' or CancellationReason__c =\'坚持继续使用\' or CancellationReason__c =\'无法修理\' or CancellationReason__c =\'修理价格太高\' or CancellationReason__c =\'用户坚持继续使用\' or CancellationReason__c =\'无法修理返品\')'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list repList) { List assids = new List(); for(Repair__c rep:repList){ assids.add(rep.Delivered_Product__r.id); } List repairs2 = [SELECT Id,name,Repair_List_Price_formula__c,Delivered_Product__r.id ,CreatedDate FROM repair__c WHERE Delivered_Product__c in :assids order by Delivered_Product__c ,CreatedDate desc]; Map assNewRepMap = new Map(); List repairs3 = new List (); for (repair__c rep : repairs2) { if (assNewRepMap.get(rep.Delivered_Product__r.id)==null){ assNewRepMap.put(rep.Delivered_Product__r.id,rep); repairs3.add(rep.id); } if (assNewRepMap.get(rep.Delivered_Product__r.id)!=null) { if (assNewRepMap.get(rep.Delivered_Product__r.id).CreatedDate < rep.CreatedDate) { assNewRepMap.remove(rep.Delivered_Product__r.id); assNewRepMap.put(rep.Delivered_Product__r.id,rep); repairs3.add(rep.id); } } } List idsss = new List(); integer i =0; List repairs4 = [SELECT id,Return_Without_Repair_Reason__c,CancellationReason__c,Delivered_Product__r.id FROM repair__c WHERE id in :repairs3 ]; for (repair__c rep : repairs4) { if (rep.Return_Without_Repair_Reason__c == 'I/F理由、確認' &&(rep.CancellationReason__c == '报价单到期' || rep.CancellationReason__c == '废弃' || rep.CancellationReason__c == '购买新品' || rep.CancellationReason__c == '零件停产' || rep.CancellationReason__c == '零件已停止生产' || rep.CancellationReason__c == '到其他公司修理' || rep.CancellationReason__c == '其他' || rep.CancellationReason__c == '修理价格高' || rep.CancellationReason__c == '坚持继续使用' || rep.CancellationReason__c == '无法修理' || rep.CancellationReason__c == '修理价格太高' || rep.CancellationReason__c == '用户坚持继续使用' || rep.CancellationReason__c == '无法修理返品')){ i++; idsss.add(rep.Delivered_Product__r.id); } } List assList = [select id ,Reson_Can_not_Warranty__c from asset where id in :idsss]; List updateList = new List(); for(Asset ass:assList){ Asset a1 = new Asset(); a1.id = ass.id; a1.Reson_Can_not_Warranty__c = ass.Reson_Can_not_Warranty__c; if (a1.Reson_Can_not_Warranty__c != null) { if (a1.Reson_Can_not_Warranty__c.contains('修理中')) { a1.Reson_Can_not_Warranty__c = a1.Reson_Can_not_Warranty__c.replace('修理中', ''); } if (!a1.Reson_Can_not_Warranty__c.contains('弃修')) { a1.Reson_Can_not_Warranty__c = a1.Reson_Can_not_Warranty__c + '弃修'; } } else { a1.Reson_Can_not_Warranty__c = '弃修'; } updateList.add(a1); } if(updateList.size()>0){ update updateList; system.debug('执行成功'); } } global void finish(Database.BatchableContext BC) { } }