1

I have a Batch method that selects all the Leads with a certain criteria, and attempts to convert them if certain details are met.

When they're successfully converted, I want to send an e-mail notification to the Account Owner of an Account that the converted lead's contact is associated with.

I'm running into a limit exception:

caused by: System.LimitException: Too many Email Invocations: 11

I can trap this exception:

try{
        Messaging.reserveSingleEmailCapacity(1);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
// ...
        } catch(Exception e){
          System.debug('Email Capacity Error. No notification sent.');
        }

But I'm wondering, some of the reading I've done seems to suggest that if use a Batchsize parameter when invoking my Batch-method, I could break the batch down to a maximum of 10 records per transaction, thus it wouldn't be possible to overflow the E-mail.

Does setting a batch-size still process all the records in my query (say with no limit clause? does it just break them into batches of 10 records?) Or is it just 10 records?

String query = 'SELECT Id,Email,Company FROM Lead '+
                    'WHERE IsConverted = false AND IsDeleted = false '+
                    'AND Email != null AND A_Bulk_Import__c = true';
2

That's probably because you are running sendEmail into a for loop.

you need to create a list of Messaging.SingleEmailMessage outside of your loop, add the SingleEmailMessage to the list, and then use sendEmail after your for loop. That way you won't need to take care of the batch size.

Messaging.SingleEmailMessage[] emails = new Messaging.SingleEmailMessage[0];

for(Lead lead:myList){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    //blabla
    emails.add(mail);
}
Messaging.sendEmail(emails);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.