tylerzika
6/7/2017 - 4:44 PM

batch.java

global void finish(Database.BatchableContext bc){
    // execute any post-processing operations

    // send an email after batch is complete to user
    // who started batch
    AsyncApexJob a = [Select Id, Status, ExtendedStatus, NumberOfErrors, 
                      JobItemsProcessed, TotalJobItems, CreatedBy.Email
                      from AsyncApexJob where Id =:bc.getJobId()];

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {a.CreatedBy.Email};

    mail.setToAddresses(toAddresses);
    mail.setSubject('Match Merge Batch ' + a.Status);
    mail.setPlainTextBody('records processed: ' + a.TotalJobItems +
                          ' with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.Email[] { mail });
}