My_Test_Repo/force-app/main/default/classes/UpdateAccount.cls

58 lines
2.0 KiB
OpenEdge ABL

global class UpdateAccount implements Database.Batchable<sObject>
{
List<Id> 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<Account> scope)
{
for(Account a : scope)
{
a.Description = 'New Description from batch class';
a.NumberOfEmployees =500;
for(Contact con:a.Contacts){
con.LastName= 'newLAstNAme';
}
}
List<Database.SaveResult> 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);
}*/
}