+ Start a Discussion
MoDiggityMoDiggity 

System.LimitException: Too many Email Invocations: 11

I'm having trouble with this because the documentation says that the limit is number of sendEmail methods called, but my trigger is bulkified and the method is only called once.

 

Does this error mean that you can only send 10 emails per apex execution?  That sounds nutso.  Here's the code (note: accountIds set was already populated)

 

// Email related vars

Messaging.SingleEmailMessage m;

string messageBody;

Messaging.SingleEmailMessage[] emails = new List<Messaging.SingleEmailMessage>();

 

Account[] accounts = [select id, name, ownerid, owner.email, type from Account where id in :accountIds];

for(Account a : accounts)

{

 if(a.type.startsWith('X') || a.type.startsWith('Pending')) { 

  messageBody = 'The account: "' + a.name + '" with type ' + a.type + ' has closed an opportunity';

  m = new Messaging.SingleEmailMessage();

  m.setHtmlBody(messageBody);

  m.setPlainTextBody(messageBody);

  m.setSubject('Pending or X account ' + a.name + ' has closed an opportunity');

  m.setToAddresses(new list<string>{a.owner.email});

  emails.add(m);

 }

 

 if(a.type == 'Distributor' || a.type == 'Distributor & Customer') {

  a.type = 'Distributor & Customer';

 } else if(a.type == 'Prospect' || a.type == 'Customer' || a.type == 'X Customer') {

  a.type = 'Customer';

 } elseif(a.type == 'CP: Consultant') {

  a.type = 'CP: Consultant & Customer';

 } else {

  a.type = 'Partner & Customer';

 }

}

 

update accounts;

 

Messaging.sendEmail(emails);

 

Does anybody know the real answer?

MaxPowerForceMaxPowerForce

It looks like you did this right. The limit is invocations of the sendEmail Method, not the number of emails sent (there are different limits for that)

 

Is it possible the trigger is being run multiple times?  Try adding something like this before the sendEmail function and reviewing the debug logs to see what is going on:

 

system.debug('@DEBUG: Email invocation count: ' + Limits.GetEmailInvocations());

jungleeejungleee
Is this trigger on account object? If yes, then same trigger will invoked multiple time as you're updating the account object.