+ Start a Discussion

Limit exceeded on batch email job

I am getting this error for a scheduled job that sends mass emails every day.

First error: SendEmail failed. First exception on row 0; first error: LIMIT_EXCEEDED, Too many target object ids.: []

Here is the code:

global with sharing class EmailSenderBatch implements Database.Batchable<SObject>, Database.Stateful 
    public String query = '';
    global Set<ID> contactIDsToSendEmailTo;
    public EmailSenderBatch()
        query = 'Select ID, Name from Account';
        contactIDsToSendEmailTo = new Set<ID>();
    global Database.QueryLocator start(Database.BatchableContext BC)
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, list<sObject> scope)
        Set<ID> accountIDs = new Set<ID>();
        for(Account a: (List<Account>) scope)
        Set<ID> accountIdsToSendEmailTo = new Set<ID>();
        for(Contract ctrct: [Select ID, AccountID, Subscription_Start_Date__c
                             from Contract 
                             where AccountID in: accountIDs
                             and Contract_Status__c =: 'Active'
                             and Subscription_Start_Date__c !=: null])
        //send every 3 months and 9 months from subscription start date
            ((ctrct.Subscription_Start_Date__c.addMonths(3).month() == (Date.today().month()))
            || (ctrct.Subscription_Start_Date__c.addMonths(9).month() == (Date.today().month())))
        //if we have qualified accounts
        if(accountIDsToSendEmailTo.size() > 0)
            for(Contact c: [Select ID, LastName, FirstName, Key_Contact__c
                            from Contact
                            where AccountID in: accountIdsToSendEmailTo
                            and Contact_Status__c =: Constants.STATUS_ACTIVE
                if(c.Key_Contact__c != null && c.Key_Contact__c.contains(Constants.CONTACTOBJ_PRIMARY_SUPPORT_CONTACT))
    global void finish(Database.BatchableContext BC)
        if(contactIDsToSendEmailTo.size() > 0)
            List<ID> contactIDs = new List<ID>();
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.setSenderDisplayName('Andrew Simpson');
            Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
    global static void startCalculation()
        EmailSenderBatch cesb = new EmailSenderBatch();

Is the error referring to the daily email limit or too many results from the query? Any help on how I could resolve the limit would be appreciated.
Krishna BandiKrishna Bandi

It looks like the error is from the number of email's in 1 batch process. Please reduce the batch size and try.
How can I limit that?
You can write one more schedulable class and call u r batch class from there


global class "Class Name" implements Schedulable{
global void execute(SchedulableContext sc){
EmailSenderBatch run =  new EmailSenderBatch();
id batchid = Database.executeBatch(run,200);