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
computing cloudcomputing cloud 

Help in If - else Condition in Batch class

Hello All ,
Based on the requirement  i have a Batch Class .
Small info needed ...in query locator i have given the conditions for all needed .Now i would like to add if - else condition for one of the condition used in query .Can i use it .
If SRR = Yes --- email should go ,If no -- stop sending email.

Here is the code ...
global class NotificationEmail implements Database.Batchable < sObject >, Schedulable, Database.Stateful {
    global List<String> errorMessages = new List<String>();
    global Database.QueryLocator start(Database.BatchableContext bc) {
        
        Date ed = Date.today().addDays(150);
        System.debug(Date.today().addDays150));
        
        set<Id> setContractIds = new set<Id>();

        for(Contract_role__c objContract: [SELECT  Contract__c FROM Contract_role__c WHERE Role__c = 'SA' AND Contract__r.EndDate =: ed]) {
            setContractIds.add(objContract.Contract__c);
        }
        
         return Database.getQueryLocator([Select  id, Contract_Name__c , EndDate ,Contact_Email__c, Contract_End_Date_2__c,  Account.Owner.Email ,Contact__r.ID FROM Contract  WHERE Id IN: setContractIds AND Renewable__c =True AND Send_Renewal_Reminder__c ='YES' ]);
    }

    global void execute(Database.BatchableContext bc, List < Contract > recs) {
        List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();
        for (Contract c: recs) {
            if (c.Contact_Email__c != null && Send_Renewal_Reminder__c ='YES' ) {
                List < String > toAddresses = new List < String > ();
                List < String > CcAddresses = new List < String > ();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
               
                ccAddresses.add(c.Account.Owner.Email);
               
                mail.setCcAddresses(CcAddresses);
                mail.setTargetObjectId(c.Contact__r.ID);
                mail.setWhatId(c.Id);
                mail.setTemplateId('00X4B000000M2i8');
                mail.setSaveAsActivity(false);
              
                mailList.add(mail);
            
}
else if (c.Contact_Email__c != null && Send_Renewal_Reminder__c ='NO')
{

}
        }
        Messaging.sendEmail(mailList);
    }

    global void finish(Database.BatchableContext bc) {
        AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()];
        
        // Send an email to the Apex job's submitter notifying of job completion.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {aaj.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSubject('JOB Salesforce Thirty Days NotificationEmailtoCustomer Finished: ' + aaj.Status);
        String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n';
        bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n';
        bodyText += 'Error Message' + String.join(errorMessages, '\n');
        mail.setPlainTextBody(bodyText);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    
    global void execute(SchedulableContext SC) {
        NotificationEmailtoAccountExecutive batchable = new NotificationEmailtoAccountExecutive();
        database.executebatch(batchable);
    }
}

Any help very much appreciated.

 
Nayana KNayana K
In querylocator fetch SRR field along with other fields :
Select SRR, rest of the fields FROM Contact WHERE id in:setContractIds AND renewable__c = true And SRR = 'YES' AND ContactEmail != null

1. In execute method you will get only records with SRR = YES and email != null so again checking if(email != null & SRR = YES) is not needed. Process recs to send email without this filter.
2. Remove elseif block for NO since control will never enter into this block.