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 

Email sender batch suddenly not working?

This code has been working perfectly for months, but suddenly it stopped. The last time the email template was used was 2/24/14. I have verified that there have been conditions where it should have been sent since then but hasn't. Any ideas?

global with sharing class NPSEmailSenderBatch implements Database.Batchable<SObject>, Database.Stateful
{
    public String query = '';
    global Set<ID> contactIDsToSendEmailTo;
   
    public NPSEmailSenderBatch()
    {
        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 6 months and 12 months from subscription start date
            if(
            ((ctrct.Subscription_Start_Date__c.addMonths(6).month() == (Date.today().month()))
            || (ctrct.Subscription_Start_Date__c.month() == Date.today().month() && ctrct.Subscription_Start_Date__c.year() < Date.today().year()))
                && ctrct.Subscription_Start_Date__c.day() == Date.Today().day()
            )
            {
                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_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('Client Care');
            mail.setTargetObjectIds(contactIDs);
            mail.setTemplateId(GenericServices.getGeneralSettingValueForKey(Constants.NPS_TEMPLATE_ID));
            Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
        }
    }
   
    global static void startCalculation()
    {
        NPSEmailSenderBatch cesb = new NPSEmailSenderBatch();
        Database.executeBatch(cesb);
    }
}
Vinita_SFDCVinita_SFDC
Hi,

It is not possible to find the issue by looking at the code. Please check audit trails of your Org to find if someone made any changes in it. Also check if you are getting any error when batch fails to run, has it been removed from scheduler?

Refer: http://help.salesforce.com/apex/HTViewSolution?id=000171199&language=en_US

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
3C3C
No changes have been made to the code since 2013, and it didn't stop functioning until February of this year. I checked the scheduled jobs and it says it is running, but no emails are being sent.