Save the Batch Error Job ID in Custom Object in case of Failure

Requirement:  Get the Failure record when processing thru batch Class

Solution : Create a custom object for saving batch log errors and create a batch class

 Custom Object: Batch_Log_Error__c
 Custom Fields: Job_id__c,Record_Id__c,Error__c

global class BatchClassName implements Database.batchable<sObject>,Schedulable{
String query ;
// define Constructor
global BatchClassName(){ 
query = 'Select id from SobjectapiName';


// Start Method Call
   global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
   } 
     //  Execute Method Call
global void execute(Database.BatchableContext bc, List<SobjectapiName> scope){
// Declare SobjectList and BatchErrorLoglst 
list<SobjectapiName> lstofsobject  = new list<SobjectapiName>();
list<Batch_Log_Error__c> lstObjBatchLogError = new list<Batch_Log_Error__c>()
try{
for(SobjectapiName obj :scope){
// Calculation 
lstofsobject.add(obj);
}
if(lstofsobject .size()>0){
Database.SaveResult[] saveResultList = Database.update(lstofsobject , false);
if(saveResultList.size()>0){
Integer i = 0;
for (Database.SaveResult objresult : saveResultList) {
if (!objresult.isSuccess()) {   
string  strErrorMessage =' ';
Batch_Log_Error__c ObjBatchLog = new Batch_Log_Error__c(Name= 'BatchClassName',Job_id__c = bc.getJobId(),Record_Id__c= lstofsobject[i].id);
Database.Error[] errs = objresult.getErrors();
if(errs.size()>0){
for(Database.Error objerror : errs){
strErrorMessage = objerror.getStatusCode() + '@@@' + objerror.getMessage();
}
ObjBatchLog.Error__c = strErrorMessage;
}
lstObjBatchLogError.add(ObjBatchLog);
}
i++;
}
}
}
}
        catch(Exception e ){    
    
            Batch_Log_Error__c ObjBatchLog = new Batch_Log_Error__c(Name= 'BatchClassName',Job_id__c = bc.getJobId(), Error__c=e.getMessage());
            lstObjBatchLogError.add(ObjBatchLog);
        }
        if(lstObjBatchLogError.size()>0){
            Database.insert(lstObjBatchLogError, false);
        }
   }   
   
// Finish Method Call
   global void finish(Database.BatchableContext info){ 

   }
   
   global void execute(SchedulableContext ctx) {  
       // For Schuldeing batch class
        BatchClassName  BatchClassNameObj =new BatchClassName ();
        ID batchprocessid = Database.executeBatch(BatchClassNameObj);
   }
}


Some Dynamic Query Example:


string  query = 'Select id,Start_Date__c,End_Date__c,Status__c,recordtypeid from SobjectAPINAME where End_Date__c <= yesterday AND Status__c !='+'\''+'Inactive'+'\''+' AND Record_Status__c !='+'\''+'Cancelled'+'\''+'';


                                Thanks,
                            Sumit Shukla
                           Mob: +919711055997
                           SkypeId: shuklasumit1

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page