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
SV MSV M 

Batch Class is not Executing

Hi, I have a batch class that will send mail if Opportunity Expiry Date (Custom Field) is Today. I have tried a batch class but it isn't working for me. Can someone tell me how to achieve this.

//Batch Class
global class OpportunityExpiryDate_New implements DataBase.Batchable <sObject> {
    global DataBase.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, Name, Expiry_Date__c FROM Oppportunity WHERE Expiry_Date__c = TODAY';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        for(Opportunity opp : scope) {
            String body = 'The Opportunity'+opp.Name+'is expiring today.';
            email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
            email.setSubject('Opportunities Expiring Today');
            email.setPlainTextBody(body);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}
Danish HodaDanish Hoda
Hi Sai,
Your batch class seesm to be fine, please check your org's deliverability and the email logs
sachinarorasfsachinarorasf
Hi Sai,

According to your condition and as per my understanding of your question. You can do this with scheduling your batch class daily and it will be checking your condition daily if any records found then it will send an email to those provided email ids.
Currently, your query doesn't found any records so your batch class is not executed. Please check the query results first. At last check your org's deliverability and receiver email id's spam section, if the receiver does not found any email in the inbox. If any problem found then let me know for providing a better solution about it.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com