Monitoring Batch Apex
Once we have submitted a Batch Apex job for processing, we will want to be able to monitor its execution to see how it is performing. Whenever we make a call to Database.executeBatch
, we are returning an Id
for the AsyncApexJob sObject
, which we can use to retrieve the status of the batch job through a query, as shown in the following snippet, that can be executed in the Execute Anonymous
window:
Id batchJobId = Database.executeBatch(new ExampleBatch()); AsyncApexJob apexJob = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors                     FROM AsyncApexJob WHERE ID = :batchJobId]; System.debug('Job has status of ' + apexJob.Status + ' and has processed ' +  apexJob.JobItemsProcessed + '/' + apexJob.TotalJobItems + ' with ' + apexJob.NumberOfErrors + ' errors.');
We can also...