Monday 11 December 2017

Batch Apex - Handle Failed Records

Batch Job status can be queried on object "AsyncApexJob"

If status is failed :
The batch failed to process the full request due to an unexpected error, such as the request is compressed with an unsupported format, or an internal server error. Even if the batch failed, some records could have been completed successfully. If the numberRecordsProcessed field in the response is greater than zero, you should get the results to see which records were processed, and if they were successful.

If any record failed to process it will not update the status as failed in AsyncApexJob.It updates as completed.

Ex:
If we are processing the 1000 records with default scope 200 records,
It executes in 5 batches and 2nd batch is having problem and failed.
Successfully executes remaining 1,3,4,5 batches.
In execute method if we use Database.Insert with opt_allOrNone as false then it will fail for only records which has issue and remaining records executes successfully in the 2nd batch .

How to handle failed records ?

1.Please verify the batch apex code and fix if any code issues.
2.If there is no issue with batch apex code then it might be data issue,Either not passed the required field values etc.
3.After fixing the data issue,Re process only failed records.Keep a flag to distinguish between successfully processed records and failed records.




No comments:

Post a Comment