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
daniel k 4daniel k 4 

Apex Batch Job Completed with Failures

Hi All,

I have an apex batch job executed with 1000 batches and 999 are successful but 1 batch failed.
My batch size was 200.
Does this mean all 200 records not processed through this batch job ?
How can I identify the records due to which, batch job is not successful ?

Thanks
D
PawanKumarPawanKumar
there is no way you can identify which one has failed. Additionally, all 200 will be failed as one transaction. But, Whoever has created the batch(CreatedBY) will be getting the developer script exception email by default. that is the only way to identify the root cause.

Regards,
Pawan Kumar
PawanKumarPawanKumar
correcting the facts:

Runtime exception messages (which is what the "Developer script execution" emails are about) always get sent to the 'Last Modified By' user of the Apex class that generates the exception. So please check who is the last modified By user and ask them to send the error email.

Regards,
Pawan Kumar
daniel k 4daniel k 4
ok, may be there is no way to find failed records from already completed job.
Let's say, I want to create a new apex batch job such that I should be able to capture the erroneous records when any of the batches failes.
In this case how can I handle ?

Thanks
D
 
PawanKumarPawanKumar
Ideally, we create custom object let sat Log__c then create a field in this let's say StackTrace long text and other fields. in the finsih method you write the final tracked log to your stack trace. This way we track log.

You can get sample code here which need to placed in finish method.
https://salesforce.stackexchange.com/questions/17569/best-practices-for-monitoring-scheduled-apex-and-batch-apex

Regards,
Pawan Kumar
PawanKumarPawanKumar
Hi Daniel,
Please help in closing by marking best. If it was helpful to you. Thanks.

Regards,
Pawan Kumar
daniel k 4daniel k 4
I think this is very old method of doing things.
I'm thinking of using stateful and capture the ids, from there I should be able to send the affected id's to my email.
 
PawanKumarPawanKumar
you go with either solution. you will have to use Database.stateful to track the ids/exception across the transactions. there is no other way.
BTW, sending only affected record ids would not be sufficient for your lead/architect/business. you send ids along with exception as well so that any can easily know what was the cause of failure of the record in any environment(stage/prod). Thanks.
daniel k 4daniel k 4
ok.
So, as in my case, if there are failures in batch job, how these records are handled ?
These 200 records should have gone through the same process as the rest of the records in the batch, but they didn't !
Also, there might be many other jobs depending on this job, in such cases how to proceed? Should we fix immediately and re-run ?
Or ignore these records for now and proceed with other jobs ?
Just want to understand how it is done in the real time

Thanks