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
Force.comForce.com 

How to return a String/Integer through Batch apex

I have a webservice which calls Batch apex for inserting chunk of records.

The issue is I want to get a success or failure message after running the Batch.

 

Is it possible to return a 'String' through Batch apex?

 

Any help much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Afzal MohammadAfzal Mohammad

You may use finish method to acheive this.

 

The Database.Batchable interface contains three methods that must be implemented

 

  1. start
  2. execute
  3. finish

The finish method is called after all batches are processed. Use this method to send confirmation emails or execute
post-processing operations.

 

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 });
}

 

Hope that helps.

 

Afzal

All Answers

Afzal MohammadAfzal Mohammad

You may use finish method to acheive this.

 

The Database.Batchable interface contains three methods that must be implemented

 

  1. start
  2. execute
  3. finish

The finish method is called after all batches are processed. Use this method to send confirmation emails or execute
post-processing operations.

 

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 });
}

 

Hope that helps.

 

Afzal

This was selected as the best answer
Force.comForce.com

Thanks for helping.

I tried the code you suggested. I am now able to send a mail consisting of status of the job and no. of batch processed i.e if I am inserting 300 records , its showing 2 batches runs successfully with 0 errors. But I need to show the no. of records per batch also and their exception if generated. 

 

Is it possible to do so?

Force.comForce.com

I can now resolve the issue, I send a status mail inside execute block that prints my custom status message.

 

Thanks for your great help.

 

I am now running into an issue as to how I could delete or cancel the existing scheduled apex job. 

My batch runs after every two hours but it throws exception due to existing apex jobs in the database with the same name.

 

I programmatically scheduled the apex job as:

system.schedue('new job',scm,m);

 

Any ideas?

 

Afzal MohammadAfzal Mohammad

You may delete a scheduled job from Setup -> Administration Setup -> Monitoring --> Scheduled Jobs.

 

Hope that helps.

 

Afzal

Force.comForce.com

Is it not possible to delete the jobs programmitically?

Afzal MohammadAfzal Mohammad

No, its not possible to delete a job programmatically.

 

Afzal

Force.comForce.com

Is it documented in salesforce?

My batch will run after every 2 hours , so it would not be possible to delete the apex job after every 2 hours.

 

How shall I resolve this issue? Plz help

Afzal MohammadAfzal Mohammad

Here is the link to faq.

http://wiki.developerforce.com/index.php/Tech_Talk:_Whats_New_in_Apex_Code_FAQ#Q:_Can_you_update_Apex_Schedule_programmatically.3F

 

However, you have an option of aborting a job.

 

Use System.abortJob method.

 

Hope that helps.

 

Afzal

Force.comForce.com

Thanks for replying.

 

I wrote system.abortJob(jobId) code inside finish method but it is still showing in scheduled job list and when I run the batch again, 'The Apex job named "New Job" is already scheduled for execution' exception gets generated.

 

Any possible workarounds for this?