Sample Code:
- global class SampleBatch Implements Database.Batchable <sObject> {
- global Database.queryLocator start( Database.BatchableContext bc ) {
- String SOQL = ‘SELECT Id, Name FROM Account LIMIT 1’;
- return Database.getQueryLocator(SOQL);
- }
- global void execute( Database.BatchableContext bc, List < sObject > scope ) {
- }
- global void finish( Database.BatchableContext bc ) {
- AsyncApexJob a = [ SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedById
- FROM AsyncApexJob
- WHERE Id =: bc.getJobId() ];
- Messaging.SingleEmailMessage batchEmail = new Messaging.SingleEmailMessage();
- batchEmail.setTargetObjectId( a.CreatedById );
- batchEmail.setSubject( ‘Batch ‘ + a.Status );
- batchEmail.setPlainTextBody( ‘Jobs processed ‘ + a.JobItemsProcessed + ‘ with ‘+ a.NumberOfErrors + ‘ failures.’ );
- batchEmail.setSaveAsActivity( false );
- Messaging.sendEmail( new Messaging.SingleEmailMessage[] {batchEmail} );
- }
- }
Output: