I have a simple requirement to write a batch which will loop through all the records for 'Object__c' and update a custom field on the object(Logic is irrelevant here).
Here is the batch class I developed:
public class SimpleBatch implements Database.Batchable<sObject>{
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id,Name,Start_Date__c,End_Date__c,Active__c FROM
Object__c WHERE Start_Date__c!=NULL AND End_Date__c!=NULL');
}
public void execute(Database.BatchableContext BC,List<Package__c> scope){
Date todayDate = Date.today();
List<Object__c> objsToUpdate = new List<Object__c>();
for(Object__c obj:scope){
if((obj.Start_Date__c <= todayDate) && (obj.End_Date__c >=todayDate)){
if(pkgObj.Active__c != 'Yes'){
pkgObj.Active__c = 'Yes';
objsToUpdate.add(obj);
}
}else if((obj.Start_Date__c > todayDate) || (obj.End_Date__c < todayDate)){
if(obj.Active__c != 'No'){
obj.Active__c = 'No';
objsToUpdate.add(obj);
}
}
}
update objsToUpdate;
}
public void finish(Database.BatchableContext BC){
//code to send email that batch finished successfully.
}
}
Lets assume there are 1000 records to process and I am running the job with a batch size of 200 records.
- Batch 1 - First set of 200 records - Records failed with validation errors.
- Batch 2 - Second set of 200 records - Records failed with validation errors.
- Batch 3 - Third set of 200 records - Records failed with validation errors.
- Batch 4 - Fourth set of 200 records - Records failed with validation errors.
- Batch 5 - Fifth set of 200 records - successfully processed, No errors.
Here is the problem: Even though first batches failed, The email is sent from the finish method saying that batch successfully processed.
Only When I look into the logs, I will know that there are some records failed otherwise everything looks to seem fine even though 4 batches failed.
Any best practices or advice on how to handle this situation.