I've just learned that finish() is never called when a batch job is aborted manually by a user from the Apex Jobs monitor. However, I would like to be able to run some logic in a batch class when its batch job is aborted manually. Is there any way to accomplish this? At the moment I can only think of either using a trigger on AsyncApexJob or firing platform events from the batch class but I am not sure if either of these will work. Any ideas?
Async apex job aborted manually from Apex Jobs Monitor - any way to catch that from the batch class?
1 Answer
AsyncApexJob does not support triggers. You might be able to use a FOR UPDATE query to obtain a lock on your current job entry to see if it's been aborted, but this is, at best, going to be a race condition. There is no reliable method to determine a hard abort when the job is cancelled from within the job.