Skip to main content

While executing it is showing no but none of debug log is visible in logs, In sandbox it is same but atlease contact is recieving an email but in production same code is executed but none of contact is getting an mail .

public class BirthDayNotificationBatchClass implements Database.Batchable<SObject>, Schedulable {

 

    // Add a class variable to store the OrgWideEmailAddressId

    private Id orgWideEmailAddressId;

 

    public BirthDayNotificationBatchClass() {

        // Initialize the OrgWideEmailAddressId in the constructor

        OrgWideEmailAddress[] owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address = 'contact@appstrail.ae'];

        if (owea.size() > 0) {

            orgWideEmailAddressId = owea[0].Id;

        }

    }

 

    public Database.QueryLocator start(Database.BatchableContext BC) {

        System.debug('Inside Start');

        Integer currentMonth = Date.today().month(); // Get current month

        Integer nextMonth = (currentMonth == 12) ? 1 : currentMonth + 1; // Calculate next month

        Integer currentYear = Date.today().year();

        System.debug('Next Month: ' + nextMonth);

        String query = 'SELECT Id, Name, Email, Birthdate, Voucher_Code__c FROM Contact WHERE CALENDAR_MONTH(Birthdate) = :nextMonth';

        System.debug('Query: ' + query);

        return Database.getQueryLocator(query);

    }

 

    public void execute(Database.BatchableContext BC, List<Contact> scope) {

        // Id templateId =  [select id, name from EmailTemplate where developername = 'Dusoul_Monthly_Birthday_Greetings'].id;

        // Move the above line outside the loop to avoid unnecessary queries

 

        for (Contact c : scope) {

            Id templateId = [select id, name from EmailTemplate where developername = 'Dusoul_Monthly_Birthday_Greetings'].id;

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            // Set the OrgWideEmailAddressId directly

            if (orgWideEmailAddressId != null) {

                mail.setOrgWideEmailAddressId(orgWideEmailAddressId);

            }

            mail.setToAddresses(new List<String>{c.Email});

            mail.setTargetObjectId(c.Id);

            mail.setTemplateId(templateId);

           

            Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

        }

    }

 

    public void finish(Database.BatchableContext BC) {

        System.debug('Batch execution finished.');

        // Optional: Add any cleanup or post-processing logic here

    }

 

    public void execute(SchedulableContext sc) {

        System.debug('Scheduled job started.');

        Database.executeBatch(this);

        System.debug('Scheduled job finished.');

    }

} #BatchClass #Send Email #Apex Class

3 Antworten
0/9000