[Salesforce]スケジュールバッチエラー時メール送信
global class TestBatch implements Database.Batchable<sObject>{
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id FROM Account');
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
throw new MyException('test');
}
global void finish(Database.BatchableContext BC){
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{'mokamoto@salesforce.com'});
mail.setReplyTo('noreply@salesforce.com');
mail.setSenderDisplayName('Batchプロセス');
mail.setSubject('Batch完了');
mail.setPlainTextBody('Batch完了しました。エラー' + a.NumberOfErrors +'件');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
class MyException extends Exception{}
}