「Too Many Email Invocations: 11」エラーについて

概要

sendEmail()メソッドを連続で11回以上で呼び出した場合に発生するエラー。

一度にメールが10件まで呼び出せないということではなく、あくまでメソッドが10回までしか呼び出せないという意味。

従って、DMLに関するガバナ制限回避時と同様、forループ外でメソッドを実行すれば問題なし。

サンプルコード

NG

for(~){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    String[] toAddresses = new String[]{'test@gmail.com'};
    String subject = 'エラーが発生しました';
    String body = 'The status code returned was not expected: '+response.getStatusCode() + ' ' + response.getStatus()+ ' ' + response.getBody();
    Id oweaId = [select id, Address, DisplayName from OrgWideEmailAddress Where DisplayName = 'test'][0].Id;

    mail.setToAddresses(toAddresses); 
    mail.setSubject(subject); 
    mail.setPlainTextBody(body); 
    mail.setOrgWideEmailAddressId(oweaId);
    mail.setUseSignature(false); 
    mail.setReplyTo('test@gmail.com'); 

    Messaging.sendEmail(mail);
}

OK

List<Messaging.SingleEmailMessage> listEmails = new List<Messaging.SingleEmailMessage>();

for(~){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    String[] toAddresses = new String[]{'test@gmail.com'};
    String subject = 'エラーが発生しました';
    String body = 'The status code returned was not expected: '+response.getStatusCode() + ' ' + response.getStatus()+ ' ' + response.getBody();
    Id oweaId = [select id, Address, DisplayName from OrgWideEmailAddress Where DisplayName = 'test'][0].Id;

    mail.setToAddresses(toAddresses); 
    mail.setSubject(subject); 
    mail.setPlainTextBody(body); 
    mail.setOrgWideEmailAddressId(oweaId);
    mail.setUseSignature(false); 
    mail.setReplyTo('test@gmail.com'); 

    listEmails.add(mail);
}
Messaging.sendEmail(listEmails);

Salesforce

Posted by regardie