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
MallikMallik 

How Batch process works during the Error.

I have been looking at other posts for my questions on how Apex batch works when there is an Error. Somehow I am not able to clear my self. I have couple of questions. Can someone please clarify.

1. Assume a batch process has 3 iterations processing 200 records each. The batch process is doing an insert and I am using Database.Insert with AllorNone parameter set to False. Assume there was an error during the second iteration while processing 101 record.I understand that all the records in the first iteration will be committed and also the 100 records in the second iteration. My question is will the batch process continue to process other records in the second iteration and also the third iteration.

2. Assume all the 3 iterations (execute method) completed without any error but some error occured in the Finish method. Finish method just sends an email. How does this work, considering that I am using Database.Insert and AllorNone parameter set to False. My understanding is that all the records will be committed to the database but user will not be receiving email that the batch was successfull or not. I am confused how this scenario will work in the real time.
Best Answer chosen by Mallik
Alexander TsitsuraAlexander Tsitsura
Hello Mallik,

1. OK. The first execute method successfully processed records, and this records committed to the database.
The second execute method successfully processed too, because of the "Database.Insert" method without the AllOrNone parameter is not throwing an exception. This method returns the list of SaveResults records. It means that successfully inserted only 199 records(1 fail #101).
But if you use the insert method or the Database.insert with AllOrNone parameter the second execute method failed, and all inserted/updated records in the second "execute" method will be rollback. And exceptions did not stop process "execute" method; this means that after an exception in the second batch execute method; the third batch "execute" method will process.

2. You are right. The start, execute and finish methods process in an asynchronous mode, and if the finish method failed, no an email message received. After each successfully processed "execute" methods, all records will have to be committed to the database. 

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hello Mallik,

1. OK. The first execute method successfully processed records, and this records committed to the database.
The second execute method successfully processed too, because of the "Database.Insert" method without the AllOrNone parameter is not throwing an exception. This method returns the list of SaveResults records. It means that successfully inserted only 199 records(1 fail #101).
But if you use the insert method or the Database.insert with AllOrNone parameter the second execute method failed, and all inserted/updated records in the second "execute" method will be rollback. And exceptions did not stop process "execute" method; this means that after an exception in the second batch execute method; the third batch "execute" method will process.

2. You are right. The start, execute and finish methods process in an asynchronous mode, and if the finish method failed, no an email message received. After each successfully processed "execute" methods, all records will have to be committed to the database. 

Thanks,
Alex
This was selected as the best answer
steve diaz 7steve diaz 7
Thanks for sharing. Really Helpful. salesforce field service lightning certification (https://www.igmguru.com/course/salesforce-field-service-lightning-training/)
Bella DainesBella Daines
Hi Malik,

In the scenario you described, where there is an error during the second iteration while processing the 101st record, the behavior will depend on how you handle the error. If you're using Database.insert with the allOrNone parameter set to false, the records processed in the first iteration (200 records) and the successfully processed records in the second iteration (100 records) will be committed to the database. However, the records in the second iteration that failed (including the 101st record) will not be committed.
The batch process will continue to process the remaining records in the second iteration (if any) and move on to the third iteration, continuing its execution until all iterations are completed or an unhandled exception occurs. So, in this case, the batch process will process the records in the second iteration that didn't encounter an error, as well as the records in the third iteration.
If all three iterations of the execute method complete without any errors, but an error occurs in the finish method, the behavior will again depend on how you handle the error. If the error in the finish method is not caught and handled, the batch job will be marked as failed, and no email will be sent.
The records processed in the execute method, including all iterations, will be committed to the database because you're using Database.insert with allOrNone set to false. However, the finish method error will prevent the successful completion of the batch job. This means that the user will not receive an email indicating whether the batch was successful or not, as the finish method was not executed successfully.
It's important to handle any potential exceptions or errors in your batch process effectively. You can consider implementing appropriate error handling, logging, and notifications to ensure that you have visibility into any failures or issues that occur during the execution of your batch job.
Please note that the behavior described above is based on the standard behavior of Apex batch processing and the usage of Database.insert with the allOrNone parameter set to false. Depending on your specific implementation and any custom logic you've added, the behavior may differ slightly.
Let me know if you have any further questions!
Shuvam PatraShuvam Patra
Hello Mallik,
  1. Yes, when you use Database.insert(records, false), it allows partial processing. So, if an error occurs while processing a record, only that particular record will fail and the rest will continue processing. In your case, even if the 101st record fails during the second iteration, the rest of the records in that iteration and the third iteration will still process.
  2. You're correct. If an error occurs in the finish() method, all the previous records that were successfully processed in the execute() method will still be committed to the database. The finish() method is usually used for post-processing activities such as sending emails or post-job logging. If the email sending fails in the finish() method, the rest of the batch execution would not be rolled back. In your scenario, all the processed records would be committed, but the user wouldn't receive an email. To handle such situations, you should wrap your email sending code in the finish() method in a try-catch block and handle the exception gracefully, possibly logging the error for further analysis and manual intervention if necessary.
datver talwerdatver talwer
During the batch process, if an error occurs, it can be handled in several ways depending on the specific situation. For example, let's consider a scenario where SlideShare Downloader is processing multiple downloads in a batch. If an error occurs while downloading a specific SlideShare presentation, the batch process can be designed to handle it gracefully. It may skip that particular presentation, log the error for troubleshooting purposes, and continue with the remaining downloads in the batch. This ensures that other SlideShare (https://slidesharedownloading.com/) files are successfully downloaded without being affected by the error, maintaining the efficiency and integrity of the overall batch process.
sdfht ytuhsdfht ytuh

Understanding the intricacies of error handling in batch processes, especially in scenarios like "How Batch Process Works During the Error," is crucial for maintaining efficiency. The insights shared here are invaluable, especially for professionals dealing with data management, like me at Chanel Shoes (https://topqualitybags.com/product-category/chanel/chanel-shoes/). Grateful for shedding light on this aspect of operations!