Skip to main content

Hi, with the below code email is not getting sent out. Can you please let me know what's missing in my code?

 

global class InvoiceReminder implements Schedulable {

    global void execute(SchedulableContext sc) {

        sendReminderMailToKAM();

    }

 

    public void sendReminderMailToKAM() {

        List<Messaging.SingleEmailMessage> masterListMails = new List<Messaging.SingleEmailMessage>();

 

        // Query invoices with Pending_POD__c

        List<Invoice__c> invoices = [SELECT Id, Invoice_Number__c, Name, CreatedDate, Hospital__r.Name, Hospital__r.AccountNumber, Hospital__r.KAM_Email_ID__c, Reminder_Date__c

                                     FROM Invoice__c

                                     WHERE Pending_POD__c > 0];

 

        // Group invoices by KAM Email

        Map<String, List<Invoice__c>> kamToInvoicesMap = new Map<String, List<Invoice__c>>();

        for (Invoice__c invoice : invoices) {

            Integer reminderDays = invoice.Reminder_Date__c != null ? Integer.valueOf(invoice.Reminder_Date__c) : 0;

 

            if (!String.isBlank(invoice.Hospital__r.KAM_Email_ID__c) && (reminderDays == 7 || reminderDays == 14 || reminderDays > 14)) {

                if (!kamToInvoicesMap.containsKey(invoice.Hospital__r.KAM_Email_ID__c)) {

                    kamToInvoicesMap.put(invoice.Hospital__r.KAM_Email_ID__c, new List<Invoice__c>());

                }

                kamToInvoicesMap.get(invoice.Hospital__r.KAM_Email_ID__c).add(invoice);

            }

        }

 

        // Iterate through KAMs and create a consolidated email

        for (String kamEmail : kamToInvoicesMap.keySet()) {

            List<Invoice__c> kamInvoices = kamToInvoicesMap.get(kamEmail);

            if (kamEmail != null && !kamInvoices.isEmpty()) {

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

                email.setToAddresses(new List<String> { kamEmail });

                email.setSubject('Reminder to create POD for the Invoices created');

 

                String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();

                String mailBody = 'Hello, <br><br>' +

                                 'The following invoices are pending POD creation:<br><br>' +

                                 '<table border="1" style="border-collapse: collapse"><tr><th>Invoice Name</th><th>Invoice Number</th><th>Created Date</th><th>Account Number</th></tr>';

 

                for (Invoice__c invoice : kamInvoices) {

                    String invoiceUrl = baseUrl + '/' + invoice.Id;

                    String invoiceName = invoice.Name;

                    String invoiceNumber = invoice.Invoice_Number__c;

                    String createdDate = invoice.CreatedDate.format();

                    String accountNumber = invoice.Hospital__r.AccountNumber;

                    mailBody += '<tr><td><a href="' + invoiceUrl + '">' + invoiceName + '</a></td><td>' + invoiceNumber + '</td><td>' + createdDate + '</td><td>' + accountNumber + '</td></tr>';

                }

 

                mailBody += '</table><br><br>';

                mailBody += 'Thank you, <br>System Admin';

 

                email.setHtmlBody(mailBody);

                masterListMails.add(email);

            }

        }

 

        if (!masterListMails.isEmpty()) {

            Messaging.SendEmailResult[] results = Messaging.sendEmail(masterListMails);

            for (Messaging.SendEmailResult result : results) {

                if (result.success) {

                    System.debug('Email was sent successfully.');

                } else {

                    System.debug('Email sending failed: ' + result.errors[0].message);

                }

            }

        }

    }

}

 

#Apex Class #Salesforce Scheduler 

1 个回答
  1. 2023年10月20日 04:20

    Hello @Ekta Khandelwal,

     

    There are a few possible reasons why this might happen, and some possible solutions that you can try.

     

    • One reason is that your scheduled apex class is not running at the specified time or frequency. This could be because you did not schedule it correctly, or because there are other scheduled jobs that are taking up the system resources. You can check and edit the schedule of your apex class by going to Setup > Monitoring > Scheduled Jobs. You can also monitor the status and logs of your apex class by going to Setup > Jobs > Apex Jobs.
    • Another reason is that your email limits are exceeded or your email deliverability settings are not configured properly. Salesforce has limits on the number of emails that can be sent per day and per hour, depending on your edition and license. You can check your email limits by going to Setup > Email Administration > Email Usage. You can also check your email deliverability settings by going to Setup > Email Administration > Deliverability.
    • A third reason is that your email logic or template is not working as expected. This could be because you have errors or typos in your code, or because you have incorrect or missing values in your fields or variables. You can debug your code by using System.debug statements and checking the debug logs. You can also test your email template by using the Send Test and Verify Merge Fields button.

    I hope this helps you.

0/9000