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){
BatchJob(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)
{
RequireContactId.add(Dog.Puppy_Parent__c);
}
}
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)) ;
DogforUpdate.add(Dog);
}
}
}
if(mailList.size() > 0)
{
Messaging.sendEmail(mailList,true);
}
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.
}
}
isEmpty
.