I'm getting a somewhat strange error message when trying to send a list of emails

System.EmailException: SendEmail failed. First exception on row 11; first error: LIMIT_EXCEEDED, System.LimitException: Too many SOQL queries: 101:

The code basically just creates a list of SingleEmailMessages from different objects and sends them together. The error message points to the Messaging.sendEmail() function.

Example Code:

for(My_Object__c obj : objectsForEmail){
        List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();


I wrote a simple tests to try to get to the bottom of this:

private static void testEmail(){
    Contact c = TestUtil.generateContact();
    c.email = TestUtil.generateRandomEmail();
    insert c;

    Messaging.SingleEmailMessage templateEmail = new Messaging.SingleEmailMessage();
    System.debug('send email XX');
    Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{templateEmail});

Debug Log

It appears somehow sending an email is somehow causing a managed package "Mass Edit Activities" to execute which in turn is causing a Task Trigger to run.

The package only runs if I send an email (tested by commenting out the email code) and after it runs, my SOQL usage jumps up from 1 query to 13!!! The trigger that it causes to run only executes 1 query, so there are 11 unaccounted for queries being caused by the managed package. I was also under the assumption that managed packages execute under a separate context of limits...

  • Are you sure the exception is due to the above code? It seems somehow there is more than 100 SOQL queries running in a single instance.
    – Saroj Bera
    Jun 30, 2015 at 6:42
  • @SarojBera the exception points to the line Messaging.sendEmail(messages); this is running inside of a trigger thought and the exception order is very complicated (multiple other triggers and worksflow triggering other triggers)
    – NSjonas
    Jun 30, 2015 at 6:46
  • 1
    What type of template is set in 'templateid'. Is is Visual force template having controller. Jun 30, 2015 at 7:12
  • @curious_techy it's a regular html/plain text template
    – NSjonas
    Jun 30, 2015 at 14:17

1 Answer 1


No, sendEmail doesn't take a SOQL query, although it does consume a DML statement.

Looking at your stack trace the extra queries are coming from the task trigger. You can use the Email.setSaveAsActivity(false); to avoid creating the Task.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .