/*2022-05-13 you * 更新产品咨询单 * 没有确认的,确认天数=今天-创建日----自动 batch * 有确认的(存在最小日期) 确认天数=最小日期-创建日-----触发器 */ global class UpdateInquiryFormConfirmationBatch implements Database.Batchable { public String query; public String inqid; private BatchIF_Log__c iflog; global UpdateInquiryFormConfirmationBatch() { this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'UpdateInquiryFormConfirmationBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; } global UpdateInquiryFormConfirmationBatch(String inqid) { this.inqid = inqid; this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'UpdateInquiryFormConfirmationBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; } global Database.QueryLocator start(Database.BatchableContext BC) { query = 'select TimeoutDays__c, Id,CreateDate__c from Inquiry_form__c where CreateDate__c != null and MinimumDate__c =null'; if (inqid != null && inqid !='') { query += ' and id = :inqid '; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list inquiryList) { system.debug('==='+inquiryList.size()); String dateToday = String.valueOf(Date.today()); for(Inquiry_form__c inq:inquiryList){ String crdate = String.valueOf(inq.CreateDate__c); String confdate=calendarUtil.getWorkDayNum(crdate,dateToday); if(Integer.valueOf(confdate) > 5){ inq.TimeoutDays__c = Integer.valueOf(confdate)-5; }else{ inq.TimeoutDays__c = null; } inq.Confirmation_days__c =null; //inq.Confirmation_days__c =Integer.valueOf(confdate); system.debug(inq.id+'=相差几天=='+confdate +'==创建日=='+crdate+'==今天=='+dateToday); } Integer indexCon = 0; if(null!=inquiryList && inquiryList.size()>0){ system.debug('inquiryList==='+inquiryList); Database.SaveResult[] lsrUpdateCon = Database.update(inquiryList, false); for (Database.SaveResult lsrChild : lsrUpdateCon) { if (!lsrChild.isSuccess()) { iflog.Is_Error__c = 3; Database.Error emsg = lsrChild.getErrors()[0]; iflog.ErrorLog__c += 'confdays: ' + inquiryList.get(indexCon).TimeoutDays__c + ' \n' + 'inquiryID: ' + inquiryList.get(indexCon).Id + '\n ConLog:' + emsg.getMessage() + '\n'; system.debug('lsrChild.isSuccess()=='+iflog.ErrorLog__c); } indexCon ++ ; } } } global void finish(Database.BatchableContext BC) { system.debug('=====iflog:' + iflog.id+'==='+iflog.ErrorLog__c); iflog.Log__c += 'UpdateInquiryFormConfirmationBatch finish()\n'; iflog.Log__c += '\nUpdateInquiryFormConfirmationBatch end'; String tmp = iflog.ErrorLog__c; if (tmp.length() > 65000) { tmp = tmp.substring(0, 65000); tmp += ' ...have more lines...'; iflog.ErrorLog__c = tmp; } String tmp2 = iflog.Log__c; if (tmp2.length() > 65000) { tmp2 = tmp2.substring(0, 65000); tmp2 += ' ...have more lines...'; iflog.Log__c = tmp2; } if (System.Label.Log_IO_Flag == 'Keep') { system.debug('jinlaile'); update iflog; } else if (System.Label.Log_IO_Flag == 'Auto') { system.debug('jinlaile1'); if (iflog.Is_Error__c > 0) { update iflog; } } } }