function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prateek GandhiPrateek Gandhi 

Batch Apex- Does finish method execute when a batch job is ABORTED

Does anyone know whether the Finish method executes or not , when the batch apex is aborted?

 

 

Abhi_TripathiAbhi_Tripathi

Hi Prateek,

 

No its not executed , you can check it out in your debug logs.

 

 

Hit KUDOS (Star) if this solution helps you.

RockzzRockzz

Hi ..

 

finish method:

global void finish(Database.BatchableContext BC){}
The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.

 

Example code:

 

global class OwnerReassignment implements Database.Batchable<sObject>{
String query;
String email;
Id toUserId;
Id fromUserId;

global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Account> accns = new List<Account>();

for(sObject s : scope){Account a = (Account)s;
if(a.OwnerId==fromUserId){
a.OwnerId=toUserId;
accns.add(a);
}
}

update accns;

}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(new String[] {email});
mail.setReplyTo('batch@acme.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}