function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
3C3C 

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)
            accountIDs.add(a.ID);
        
        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
            if(
            ((ctrct.Subscription_Start_Date__c.addMonths(3).month() == (Date.today().month()))
            || (ctrct.Subscription_Start_Date__c.addMonths(9).month() == (Date.today().month())))
            )
            {
                accountIDsToSendEmailTo.add(ctrct.AccountID);
            }
        }
        
        //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))
                    contactIDsToSendEmailTo.add(c.ID);
            }
        }
    }
    
    global void finish(Database.BatchableContext BC)
    {
        if(contactIDsToSendEmailTo.size() > 0)
        {
            List<ID> contactIDs = new List<ID>();
            contactIDs.addAll(contactIDsToSendEmailTo);
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.setSenderDisplayName('Andrew Simpson');
            mail.setTargetObjectIds(contactIDs);
            mail.setTemplateId(GenericServices.getGeneralSettingValueForKey(Constants.TEMPLATE_ID));
            Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
        }
    }
    
    global static void startCalculation()
    {
        EmailSenderBatch cesb = new EmailSenderBatch();
        Database.executeBatch(cesb);
    }
}

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
Hi,

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

Ex: 

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