I'm trying to figure out why I am getting the following error when I try to run a batch job. I thought I coded it correctly (I don't see any queries inside a for loop) but I must have missed something.

System.EmailException: SendEmail failed. First exception on row 50; first error: LIMIT_EXCEEDED, System.LimitException: npsp:Too many DML statements: 151: []

Here is the code

global class Dog_PuppyParent_Email_Monthly implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){      
      RecordType rt = [SELECT Id FROM RecordType WHERE DeveloperName = 'In_Training' AND sobjecttype = 'Dog__c' LIMIT 1];
      return Database.getQueryLocator([select OwnerId,Owner.Email,id,Name,Puppy_Parent__c,RecordTypeID,Last_Puppy_Parent_Eval_Sent__c,Last_Puppy_Parent_Eval_Received__c from Dog__c where Puppy_Parent__c != NULL and RecordTypeId = :rt.id]);

global void execute(Database.BatchableContext BC, List<sObject> scope){

global void finish(Database.BatchableContext BC){

public STATIC void  BatchJob(List<sObject> scope)
    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
    List<Dog__c> DogforUpdate = new List<Dog__c>(); 
    List<ID> RequireContactId = new List<ID>();
    String oweaId = '0D2j00000008OUY';

   for(sobject theS : scope){            
        Dog__c Dog = ((Dog__c)theS);
        if(Dog.Puppy_Parent__c != NULL)

   Map<ID,Contact> ContactData = new Map<ID,Contact>([SELECT Id,Email FROM Contact WHERE Id in :RequireContactId]);

   for(sobject s : scope){            
        Dog__c Dog = ((Dog__c)S);       
        if(Dog.Puppy_Parent__c != NULL && ContactData.containsKey(Dog.Puppy_Parent__c))
            Contact Con = ContactData.get(Dog.Puppy_Parent__c);
            if(Con.Email != NULL){
                Dog.Last_Puppy_Parent_Eval_Sent__c = system.today();  
                Dog.Last_Puppy_Parent_Eval_Received__c = NULL; 
                String TemplateId = '00Xj0000000QAnc';             
                mailList.add(CreateEmail(Con,TemplateId,oweaId,Dog)) ;           
     if(mailList.size() > 0)

     if(DogforUpdate.size() > 0)
         UPDATE DogforUpdate;

Public Static  Messaging.SingleEmailMessage CreateEmail(Contact c ,String TemplateId ,String OrgwideEmailId, Dog__c d)   {
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); //create new email variable
   mail.setTemplateId(TemplateId );    //set Email template id for this email
   mail.setOrgWideEmailAddressId(OrgwideEmailId);  //Org wide email for sender address it must need to be verified.
   mail.setTargetObjectId(c.id);   ///setting contact id as who id.
   mail.setwhatid(d.id);    //setting dog id as what id.        
   return mail;                    //return this email for adding this in to list or send it directly.

  • 1. Don't use hard-coded Id values. 2. You don't ever need to check isEmpty.
    – Adrian Larson
    Oct 6, 2016 at 0:31
  • I still get the same error.
    – Jennifer
    Oct 6, 2016 at 1:16
  • Yeah neither of those will resolve the error, but are considerations you should remember with every class you write on the platform.
    – Adrian Larson
    Oct 6, 2016 at 1:17
  • You most likely have a loop with some DML inside it in your triggers on Dog__c.
    – sfdcfox
    Oct 6, 2016 at 2:31
  • Try by giving limit less than equal to 200 for batch execute method. Oct 6, 2016 at 6:50

1 Answer 1


Not sure if that is the complete error message or exactly how it looks but it appears:

  1. Your code looks ok
  2. Means it is coming from somewhere else and the stack trace goes back to your sendEmail call

So, if it is not is your code and it is in the npsp you will have to reduce your batch scope to a value that does not cause the error, start at 50 and work down. Depending on need you can even start at 1 and god up. Evaluate your need to do multiple scope size at a time (how much data is being processes) and if not that much just stick with 2

  • Thank you! I just lowered it from 100 to 50 and it's working now. I had tried removing all my triggers on Dog and that didn't work so I think it was/is the NPSP.
    – Jennifer
    Oct 6, 2016 at 16:22
  • @Jennifer - The giveaway that is was the NPSP was the npsp in the error message
    – Eric
    Oct 6, 2016 at 18:31

You must log in to answer this question.

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