global class UpdateFSEApplyForRepairTimeBatch implements Database.Batchable { public String query; public List ids; public Integer year; global UpdateFSEApplyForRepairTimeBatch(List tempIds) { this.query = query; this.ids = tempIds; } global UpdateFSEApplyForRepairTimeBatch() { this.query = query; } global UpdateFSEApplyForRepairTimeBatch(Integer tempyear) { this.query = query; this.year = tempyear; } global Database.QueryLocator start(Database.BatchableContext bc) { query = 'SELECT Id,FSE_ApplyForRepair_time__c,SAP_Transfer_time__c FROM Repair__c '; if (ids!= null && ids.size() > 0) { query += ' where Id in : ids'; }else if (year != null) { Date startDay = Date.newInstance(year,4,1); Date tempDay = Date.newInstance(year+1,4, 1); Date endDay = tempDay.addDays(-1); query += ' WHERE SAP_Transfer_day__c >= :startDay and SAP_Transfer_day__c <= :endDay'; query += ' and SAP_Transfer_time__c != null and FSE_ApplyForRepair_time__c = null'; } else{ query += ' WHERE SAP_Transfer_time__c != null and FSE_ApplyForRepair_time__c = null'; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { if (scope!=null && scope.size() > 0) { for (Repair__c rc : scope) { rc.FSE_ApplyForRepair_time__c = rc.SAP_Transfer_time__c; } Database.update(scope); } } global void finish(Database.BatchableContext BC) { } }