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
K RoyK Roy 

How can i reprocess failed records from a batch?

I have a requirement where i have to reprocess the failed records in batch apex. Please help me with the process.
Arun Kumar 1141Arun Kumar 1141
Hi, 
You can refer the following link:-
https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/asynch_api_batches_failed_records.htm

Please mark it as best answer if this helps.
Thanks
Beatrice Hill 2Beatrice Hill 2
To reprocess failed records in a batch Apex job, you'll need to implement a retry mechanism in your execute method. Here's an example of how you can do that:

Create a new custom field on the object that you're processing in the batch job. This field should be a number field to keep track of the number of times the record has been retried.

In your execute method, retrieve the records that failed using the Database.QueryLocator or Database.getQueryLocator method.

Iterate through the failed records, and for each record:

a. Check the value of the custom retry field. If it is less than a certain threshold (e.g. 5), proceed with retrying the record.

b. If the value of the retry field is greater than or equal to the threshold, skip the record and log an error message.

c. If the record is successfully processed on a retry attempt, reset the retry field to 0.

To retry a failed record, you can simply call the method that processes the record again. You may need to modify this method to handle retries and update the retry field on the record.

Schedule the batch Apex job to run again after a certain period of time (e.g. 1 hour) to retry any records that still have a non-zero retry field value.
SwethaSwetha (Salesforce Developers) 
HI Roy,
I'm not sure of your use case but basically, you need to identify the root cause of the failure and fix it before rerunning the batch job to prevent another failure.

Try below approach
1. Catch the exception: Wrap the code inside your execute method in a try-catch block to catch any exceptions that are thrown.
2. Add the failed record to a list: If an exception is caught, add the failed record to a list of failed records.
3. Complete the batch execution: After the execute method completes, implement the finish method to handle any remaining records and perform any cleanup tasks.
4. Create a new batch job with failed records: Create a new batch job and include only the failed records from the previous job. You can do this by creating a query in the start method of your batch class to select only the failed records based on their IDs.
5. Execute the new batch job: Execute the new batch job to reprocess the failed records.

Example snippet for reference:
 
global void execute(Database.BatchableContext BC, List<Account> scope) {
    List<Account> failedRecords = new List<Account>();
    try {
        // Your code to process each account record in the scope

    } catch (Exception ex) {
        // If an exception is caught, add the failed record to the list of failed records
        failedRecords.add(scope[0]);
    }
    
    // Complete the batch execution

}

global void finish(Database.BatchableContext BC) {
    // Handle any remaining records and perform cleanup tasks

    // Create a new batch job with failed records
}
How To Invoke Batch Apex From Another Batch Apex In Salesforce:
https://www.emizentech.com/blog/call-batch-apex-from-another-batch-apex.html
https://www.biswajeetsamal.com/blog/invoke-batch-apex-from-another-batch-apex/

Resubmit Failed Records
https://help.salesforce.com/s/articleView?id=release-notes.rn_resubmit_records.htm&release=232&type=5

https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/asynch_api_batches_failed_records.htm

If this information helps, please mark the answer as best. Thank you
Angeline GarrettAngeline Garrett
According to uno online (https://uno-online.io), to reprocess failed records in a batch Apex job, you’ll need to implement a retry mechanism in your execute method. Here’s an example of how you can do that:
Create a new custom field on the object that you’re processing in the batch job. This field should be a number field to keep track of the number of times the record has been retried.
In your execute method, retrieve the records that failed using the Database.QueryLocator or Database.getQueryLocator method.
Iterate through the failed records, and for each record: a. Check the value of the custom retry field. If it is less than a certain threshold (e.g. 5), proceed with retrying the record. b. If the value of the retry field is greater than or equal to the threshold, skip the record and log an error message. c. If the record is successfully processed on a retry attempt, reset the retry field to 0.
To retry a failed record, you can simply call the method that processes the record again. You may need to modify this method to handle retries and update the retry field on the record.
Schedule the batch Apex job to run again after a certain period of time (e.g. 1 hour) to retry any records that still have a non-zero retry field value.