1

I am run a batch class from a normal class using Database.executeBatch method, steps below the batch execute methods are executing before the Batch process is completed.

Does there is any way to capture to get the acknowledgment as the batch process is completed.

Below is my code:

ID batchprocessid = Database.executeBatch(new BatchDeleteRcds(), 2000);
System.debug('ID::::::::::::::'+batchprocessid);
AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID =: batchprocessid ];
System.debug('Batch processing : '+aaj.NumberOfErrors); 
2

In the finish method you can get your information. Can send some email or something like that.

global void finish(Database.BatchableContext BC){
   // Get the ID of the AsyncApexJob representing this batch job
   // from Database.BatchableContext.
   // Query the AsyncApexJob object to retrieve the current job's information.
   AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
      TotalJobItems, CreatedBy.Email
      FROM AsyncApexJob WHERE Id =
      :BC.getJobId()];
   // Send an email to the Apex job's submitter notifying of job completion.
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {a.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + a.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + a.TotalJobItems +
   ' batches with '+ a.NumberOfErrors + ' failures.');
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

More help can be found here

1

Why don't you try to get your final work on onFinish method of the Batch class. Where you can check whether your batch ran successfully or not.

  • can you give me reference where the onFinish method is used in my code. – Vivek P Aug 4 '15 at 10:21
  • You have a class "BatchDeleteRcds" which probably implementing Database.Batchable. So in your class i.e. "BatchDeleteRcds" you can write a method 'onFinish(Database.BatchableContext BC)' – Vivek Malpani Aug 4 '15 at 10:29
  • Use this as a reference developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Vivek Malpani Aug 4 '15 at 10:30
  • do you mean from finish method i need to call the other method of initial Apex class write. if that so, does there is any other approach for this ? – Vivek P Aug 4 '15 at 10:40
  • I am saying you can use like this in FINISH method AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID =: BC.getJobId() ]; System.debug('Batch processing : '+aaj.NumberOfErrors); – Vivek Malpani Aug 4 '15 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.