global class AgencyHospitalLinkBatch implements Database.Batchable { public String query; private BatchIF_Log__c iflog; global AgencyHospitalLinkBatch() { this.query = query; iflog = new BatchIF_Log__c(); iflog.Type__c = 'AgencyHospitalLinkUpdateError'; iflog.Log__c = 'AgencyHospitalLinkBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; } global Database.QueryLocator start(Database.BatchableContext bc) { //sql语句 query = 'select Id, Name,Hospital__c,Hospital_ID__c,Agency__c, Hospital_Name_readonly__c,'; query += 'Hospital_Recordtype__c from Agency_Hospital_Link__c where isSame__c =\'0\' '; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { List agencyhlList = new List(); List updAgencyhlList = new List(); agencyhlList = scope; for (Agency_Hospital_Link__c agencyhl : agencyhlList) { agencyhl.Name = agencyhl.Hospital_Name_readonly__c; updAgencyhlList.add(agencyhl); } /*if (updAgencyhlList.size() > 0) { update updAgencyhlList; }*/ if(updAgencyhlList.size() > 0){ Database.SaveResult[] lsr = Database.update(updAgencyhlList, 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 ' + updAgencyhlList[tIdx].Id + ' Agency_Hospital_Link__c:' + emsg + '\n'; } } //update updAgencyhlList; } } global void finish(Database.BatchableContext BC) { iflog.Log__c += '\nAgencyHospitalLinkBatch 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; } }