global class NextMonthVisitBatch implements Database.Batchable, Database.Stateful { //HWAG-BML3AC 询价任务创建 精琢技术 wql 2020/04/10 start //soql语句 private String query; //用于单条传入id更新询价 private String oppoId; //用于批量传入id更新询价 private List oppoList; //用于执行batch同时记录日志信息 private BatchIF_Log__c iflog; Boolean IsNeedExecute = false; // 2021-03-09 mzy WLIG-BYHD79 SFDC环境batch合并调查 是否符合执行条件 global NextMonthVisitBatch() { // this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'NextMonthVisitBatch start1\n'; iflog.ErrorLog__c = ''; insert iflog; } // 2021-03-09 mzy WLIG-BYHD79 SFDC环境batch合并调查 start global NextMonthVisitBatch(Boolean NeedExecute) { this.IsNeedExecute = NeedExecute; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'NextMonthVisitBatch start1\n'; iflog.ErrorLog__c = ''; insert iflog; } // 2021-03-09 mzy WLIG-BYHD79 SFDC环境batch合并调查 end global NextMonthVisitBatch(String oppoId) { this.oppoId = oppoId; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'NextMonthVisitBatch start2\n'; iflog.ErrorLog__c = ''; insert iflog; } global NextMonthVisitBatch(List oppoList) { this.oppoList = oppoList; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'NextMonthVisitBatch start3\n'; iflog.ErrorLog__c = ''; insert iflog; } // global NextMonthVisitBatch(boolean refresh) { // // this.query = query; // iflog = new BatchIF_Log__c(); // iflog.Type__c = 'PushNotification'; // iflog.Is_Error__c = 0; // iflog.Log__c = 'NextMonthVisitBatch start1\n'; // iflog.ErrorLog__c = ''; // insert iflog; // thisMonthExecuteFlag = refresh; // } global Database.QueryLocator start(Database.BatchableContext bc) { // String query ='select id from Opportunity where IsNextMonthOfVisit__c =true'; //String query ='select id from Opportunity where IsNextMonthOfVisit__c =true OR IsThisMonthOfVisit__c = true'; //20201214 zh CHAN-BW35VV String query ='select id from Opportunity where (IsNextMonthOfVisit__c =true OR IsThisMonthOfVisit__c = true)'; // 2021-03-10 mzy WLIG-BYHD79 if (!string.isblank(oppoId)) { query += ' and id = :oppoId'; } if (oppoList != null) { query += ' and id in :oppoList '; } // 2021-03-09 mzy WLIG-BYHD79 SFDC环境batch合并调查 start // 每月最后一天 运行 不满足运行时间,可以使查询到的结果为空 //获取这个月的最后一天 Date toDate = Date.today(); Date days = Date.newInstance(toDate.year(), toDate.month() + 1, 0); Integer monDays = days.day(); Date dayEnd = Date.newInstance(toDate.year(), toDate.month(), monDays); if(toDate != dayEnd && IsNeedExecute == true){ query = 'Select Id from Opportunity where Name = \'\' AND Name != \'\' '; } //2021-03-09 mzy WLIG-BYHD79 SFDC环境batch合并调查 end return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scList) { try{ List opportunityList = new List(); for(Opportunity o:scList){ Opportunity oppo = new Opportunity(); oppo.Id = o.Id; oppo.IsNextMonthOfVisit__c = false; oppo.IsThisMonthOfVisit__c = false;//20201214 zh CHAN-BW35VV opportunityList.add(oppo); } if (opportunityList.size() > 0){ Database.SaveResult[] lsr = Database.update(opportunityList, false); for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) { Database.SaveResult sr = lsr[tIdx]; System.debug('sr.isSuccess:' + sr.isSuccess()); if (!sr.isSuccess()) { Database.Error emsg = sr.getErrors()[0]; iflog.ErrorLog__c += 'ERROR ' + opportunityList[tIdx].Id + ' Opportunity:' + emsg + '\n'; } } // update opportunityList; } }catch(Exception e){ iflog.ErrorLog__c += 'ERROR : Opportunity:' + e.getMessage() + '\n'; } } global void finish(Database.BatchableContext BC) { //更新该日志的数据信息 iflog.Log__c += '\nNextMonthVisitTwoBatch end'; String tmp = iflog.ErrorLog__c; if (tmp.length() > 65000) { tmp = tmp.substring(0, 65000); tmp += ' ...have more lines...'; iflog.ErrorLog__c = tmp; } update iflog; } //HWAG-BML3AC 询价任务创建 精琢技术 wql 2020/04/10 end }