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
Aniketh KattimaniAniketh Kattimani 

Send email if checkbox is false

Hi Guys,

I am new to developing and needed some assistance in understand this error message which i get when i execute the below mentioned batch to send email notification on daily basis if checkbox is false (Which includes some more conditions :

Requirement 1 : Build a nightly job to send an Email to the Primary Contact Email address on the opportunity if any of the following checkboxes are not checked
Section 1  
Section 2
Section 3
Section 4
Section 5
Requirement 2 : Section 5 is not required for opportunities having a stored date prior to 10/1/2013
Requirement 3 : 
Emails are triggered based on the following:
Enrollment date is < current date && Storage date = Null && DueDate = CurrentDate+14
OR
Enrollment date != Null && Storage date = CurrentDate + 7
global class OpportunityEnrollmentAlert implements Database.Batchable<sObject>,Database.Stateful{
    
APEX Code :
global List <String> emailAdd = new List<String>();
    global final Date DueCheck = Date.newInstance(2013, 10, 1);
    List<Messaging.SingleEmailMessage> mails = new  List<Messaging.SingleEmailMessage>();
    global Database.Querylocator start(Database.BatchableContext BC){
        
        String query = 'SELECT Id, Name, Primary_Contact__c, Primary_Email__c, Enrollment_Date__c, Storage_Date__c from Opportunity where (Section_1_Received__c = FALSE OR Section_2_Received__c = FALSE OR Section_3_Received__c = FALSE OR Section_4_Received__c = FALSE OR Section_5_Received__c = FALSE && objScope.Storage_Date__c > DueCheck)';
        system.debug('query==========>>>>>>>'+query);
        return Database.getQueryLocator(query);
        
    }
    
    global void execute(Database.BatchableContext BC, List<Opportunity> scope){
        system.debug('Inside execute method');
        system.debug('scope=====>>>>>'+scope);
        set<id> oppIdList = new set<id>();
        
        for (Opportunity objScope: scope){
            
            if (objScope.Enrollment_Date__c < Date.Today() && objScope.Storage_Date__c == NULL && objScope.Due_Date__c == Date.Today().addDays(14)){
                System.debug('_------------------Test');
            }
            else if(objScope.Enrollment_Date__c != NULL && objScope.Storage_Date__c == Date.Today().addDays(7)){
            }
            oppIdList.add(objscope.id);
        }
        
        if (oppIdList!= null && oppIdList.size()>0){
            
        }
        
        if(scope!=null || !scope.isEmpty()){
            for(Opportunity opp:scope){
                system.debug('primary contact email'+opp.Primary_Email__c);
                
                // Step 1: Create a new Email
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                
                // Step 2: Set list of people who should get the email
                emailAdd.add(opp.Primary_Email__c);
                message.setToAddresses(emailAdd);
                
                // Step 4. Set email contents - you can use variables!
              
                Id etId = [select id, Name from EmailTemplate where developername='Missing_Enrollment_Forms'].id;
                message.setTargetObjectId(opp.Primary_Contact__c);
                message.setTemplateId(etId);
                mails.add(message);    
            }
        }
        // Step 6: Send all emails in the master list
        Messaging.sendEmail(mails);
    }
    global void finish(Database.BatchableContext BC){    
    }
}
Error : 
User-added image

Appreciate all your help.
Best Answer chosen by Aniketh Kattimani
Shubham4462Shubham4462
Hello Aniketh ,

Define your Line No. 3  List<Messaging.SingleEmailMessage> mails = new  List<Messaging.SingleEmailMessage>();

inside execute method then try it.

All Answers

Shubham4462Shubham4462
Hello Aniketh ,

Define your Line No. 3  List<Messaging.SingleEmailMessage> mails = new  List<Messaging.SingleEmailMessage>();

inside execute method then try it.
This was selected as the best answer
Aniketh KattimaniAniketh Kattimani
Thanks @Shubham, That worked.
I have one more query, do you feel the condition in the code (Line 17-28) meets the requirement 2 mentioned above according to you?
Shubham4462Shubham4462
Hie Aniketh i am not able to understand the scenrio can you elobrate it.
Aniketh KattimaniAniketh Kattimani
Requirement 2 : Section 5 is not required for opportunities having a stored date prior to 10/1/2013

So when section 5 is not selected, it should take only those opportunities which have a storage(Custom field) date of 1/10/2013 in the list and then send out email notification to Primary contact of that opportunity.
Can you let me know if the (Line 17-28)  in the code satisfies the condition.