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
Soubhagya Ranjan 2Soubhagya Ranjan 2 

how to send mail to user for the failed records in batch apex

Hi all 

My requirement is to insert records into account object through batch apex . there are some validations in account objects . so some records can not insert in to it . so at the last step of batch apex i want to send email to users of those failed record to modify the record as per requirement . how will i achieve this . can any one provide me the code for this .

Hi Soubhagya Ranjan,
These are some point of focus
1.You have to take flag like "isUpdate" chekbox type. default it should be unchecked and when you insert or update record then this flag should also checked or updated.
3.When Batch job is executing then this flag is also update and if any record are failed in insert or update  then flag is not update.
4.then you check using soql  and send them mail where flag in unchecked.

I think this help you.
Hi Soubhagya ,

You have to add Database.stateful in your class signature which will maintain the state of the variable till end of the batch .
ex :global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

You need to declare a List variable in class label to hold the error detail .
Instead of plain insert use Database.insert() .
Check below link it will help to capture the error .  

You can get the error record detail to the list and finally in finish method you can send email to whom you want to send .

I think this will help you to finish the task ,let me know incase any issue .