global class UpdateAccount implements Database.Batchable { List failedUpdates; global Database.QueryLocator start(Database.BatchableContext BC) { String query1 = 'SELECT Id,Description, NumberOfEmployees,(SELECT LastName FROM Contacts) FROM Account'; return Database.getQueryLocator(query1); } global void execute(Database.BatchableContext BC, List scope) { for(Account a : scope) { a.Description = 'New Description from batch class'; a.NumberOfEmployees =500; for(Contact con:a.Contacts){ con.LastName= 'newLAstNAme'; } } List results = Database.update(scope, false); for (Database.SaveResult result : results) { if (!result.isSuccess()) { failedUpdates.add(result.getId()); } } } global void finish(Database.BatchableContext BC){ if (!failedUpdates.isEmpty()) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] {'himanshu.s@hicglobalsolutions.com'}); mail.setSubject('Batch Apex Failure'); String messageBody = 'The following records failed to update: '; for (Id failedId : failedUpdates) { messageBody += failedId + ', '; } mail.setPlainTextBody(messageBody); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); } } /*global void execute(SchedulableContext SC) { UpdateAccount batchJob = new UpdateAccount(); Database.executeBatch(batchJob); } public static void scheduleJob() { UpdateAccount batchJob = new UpdateAccount(); String cronExpression = '0 0 0 ? * MON *'; // Every Monday at midnight System.schedule('Update Account Batch', cronExpression, batchJob); }*/ }