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
Richard Rodriguez 1Richard Rodriguez 1 

Daily emails based on date field in Account object (Apex Class) - help?

I'm trying to create an Apex Class that checks all Account records on a daily basis and sends an email to the email field on ones that have a review due on that date.

For example:
Vendor ABC Pty Ltd has opted in for reviews (Vendor Review: Reminders = true), has an annual review date set in their record (Vendor Review: Date = 17/02/2019), and an email address set in their record (Vendor Review: Email = abc@test.com).

Here is my code:
global class VendorReviewCronJob implements Schedulable{ 
    
        global void execute(SchedulableContext SC) {
            sendmail();
        }

        public List<Id> getVendorReviewEmailAddresses(Integer Month, Integer Day) 
        { 
            List<Id> mailToIds = new List<Id>();
             
            Account[] a = [SELECT Id, Vendor_Review_Email__c, Vendor_Review_Date__c, Vendor_Review_Reminders__c
                            FROM Account 
                            WHERE DAY_IN_MONTH(Vendor_Review_Date__c) = : Day 
                            AND CALENDAR_MONTH(Vendor_Review_Date__c) = : Month   
                            ];
        
            for(Account recipient : a) {
                    
                    System.Debug('\n*******Found VendorReview Recipient');
                                        
                    if (recipient.Vendor_Review_Reminders__c == true)
                    {
                        mailToIds.add(recipient.Id);
                        System.Debug('\n*******Recipient: '+ recipient.Vendor_Review_Email__c);
                         
                    } else {
                        System.Debug('\n*******NO Recipient');
                    }
                
            }

            return mailToIds;
        }




        public void sendMail() 
        {
      
            String debugAddress = 'eyewell@salesforce.com';
            String VendorReviewEmailTemplateName = 'User_Vendor_Review_Required';       
            String debugMessage;
            String[] toAddresses;

            Integer DayOfEvent   = date.today().day();
            Integer MonthOfEvent = date.today().month();

            List<Id> VendorReviewIdsList = getVendorReviewEmailAddresses(MonthOfEvent,DayOfEvent);

            EmailTemplate VendorReviewTemplate = [select Id,Name,Subject,body from EmailTemplate where DeveloperName = :VendorReviewEmailTemplateName];
 
            if(VendorReviewTemplate != null && VendorReviewIdsList.isEmpty() == false)
            {

                Messaging.MassEmailMessage VendorReviewMail = new Messaging.MassEmailMessage();
    
                VendorReviewMail.setTargetObjectIds(VendorReviewIdsList);
                VendorReviewMail.setTemplateId(VendorReviewTemplate.Id);
                VendorReviewMail.setUseSignature(false);
                VendorReviewMail.setSaveAsActivity(true);

                try {
                    Messaging.sendEmail(new Messaging.MassEmailMessage[] { VendorReviewMail });
                }catch(Exception e)
                {
                    System.Debug(e);
                }
           
            }
            else
            {
                System.Debug('VendorReviewCronJob:sendMail(): Either an email template could not be found, or no Account has a Vendor Review today');
            }

                
        }

    
}
I've scheduled the apex class to run daily, but although it's showing that it has run, the emails for the reviews due that day haven't sent, and I also haven't received the confirmation email of how many were sent.

Admittedly, I got the base of the code from a mailer that was checking for birthdays on a Contact object, but I felt the same principles still applied.

Can anybody see where I've gone wrong?
Best Answer chosen by Richard Rodriguez 1
SarvaniSarvani
Hello Richard,

I see there is no problem in your code. It worked for me, when I tested on "contact" object but NOT  for "Account" object. It gave me error "Only Users, Contacts, Leads, and Person objects are allowed for targetObjectIds.: []."  Try using SingleEmailMessage method and use setToAddresses attribute to set the email receiver.

Hope it helps,

Thanks,
Sarvani

All Answers

Manj_SFDCManj_SFDC
Hi Richard,
you can try these,
From Setup, enter Deliverability in the Quick Find box, then select Deliverability, set the Access Level to All Email 
From Setup enter Email Delivery Settings and check if there are any Email relays configured and any Emai ldomain filters are set 
or request for an email log , 
https://help.salesforce.com/articleView?id=email_logs_edit.htm&type=5
good luck !
Please mark the question as solved, if this answer helps you 
Richard Rodriguez 1Richard Rodriguez 1
Hello @Manj_SFDC.
Thank you for your suggestions, however, Access to Send Email (All Email Services) was already set to "All emails" and there were no Email Relays. Still no resolution.
SarvaniSarvani
Hello Richard,

I see there is no problem in your code. It worked for me, when I tested on "contact" object but NOT  for "Account" object. It gave me error "Only Users, Contacts, Leads, and Person objects are allowed for targetObjectIds.: []."  Try using SingleEmailMessage method and use setToAddresses attribute to set the email receiver.

Hope it helps,

Thanks,
Sarvani
This was selected as the best answer
Richard Rodriguez 1Richard Rodriguez 1
Thank you, Sarvani. I think you've cracked it. I wasn't aware that this method wasn't allowed on a non-Contact object.