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
s k 85s k 85 

Batch class to send email notification to each account(clinic)

Batch class to send email notification to each account(clinic) 
I tried writing a batch class but it is not working fine. Can someone look and help me out. My requirement is below:
1) Batch job will run every day.
2)it will send email to each accoun(clinc) regarding the animal vaccination due date before 15 days and 30 days like [duedate = today() +30 days]
3)clinic will get email that "folloing are the vaccination due date detail you chose for monthly alert:
    1. Vaccination1  Due date: 28/10/2018
    2. Vaccination2 due date   28/10/2018

Please help me:


global class sendEmailToAcc implements Database.Batchable<sObject>
{
    
    Global Transient List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        DateTime dT = System.now()+30;
        Date MonthDate = date.newinstance(dT.year(), dT.month(), dT.day());
        
        DateTime dT1 = System.now()+15;
        Date fortdate = date.newinstance(dT1.year(), dT1.month(), dT1.day());
        
        String mon = 'Monthly';
        String Fnight = 'Fortnightly';
        
        String SelectClause = 'SELECT Id,Name,Due_Date__c, vcc.Clinic__r.Name, vcc.Clinic__r.Email__c, vcc.Clinic__r.Email_Preference__c FROM VaccinationRegister__c  vcc';
        String WhereClause = '(vcc.Clinic__r.Email_Preference__c =:mon  AND Due_Date__c  =: MonthDate ) OR (vcc.Clinic__r.Email_Preference__c =:Fnight AND Due_Date__c  =: fortdate)';

        String query = SelectClause +' '+ 'WHERE ' + WhereClause ;
        System.debug('query--'+query);
        
                
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<VaccinationRegister__c> scope)
    {
        System.debug('scope--'+scope);
        
        
        Id templateId=[SELECT Id,Name FROM EmailTemplate where name = 'SendEmailtoAccount'].Id;
        EmailTemplate templ=[Select Id,body from EmailTemplate where Id=:templateId];
        mails = new List<Messaging.SingleEmailMessage>();
        for(VaccinationRegister__c scp : scope){
            
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setSubject('Vaccination available are');
            message.setToAddresses(new List<String>{scp.Clinic__r.Email__c});
            
            
            message.setSaveAsActivity(false);
            message.setTemplateId(templateId);
            templ.body=templ.body.replace('{!VaccinationRegister__c.Clinic__r.Name}', scp.Clinic__r.Name);
            templ.body=templ.body.replace('{!VaccinationRegister__c.Name}', scp.Name);
            templ.body=templ.body.replace('{!VaccinationRegister__c.Due_Date__c}', string.valueOf(scp.Due_Date__c));
            message.setPlainTextBody(templ.body);
            mails.add(message);
        }
        Messaging.sendEmail(mails);
        
    }
    global void finish(Database.BatchableContext BC) {
    }
}
 
Raj VakatiRaj Vakati
Change your code as below in the start method 
 
DateTime dT = System.now()+30;
Date MonthDate = date.newinstance(dT.year(), dT.month(), dT.day());

DateTime dT1 = System.now()+15;
Date fortdate = date.newinstance(dT1.year(), dT1.month(), dT1.day());

String mon = 'Monthly';
String Fnight = 'Fortnightly';

String SelectClause = 'SELECT Id,Name,Due_Date__c,  Clinic__r.Name,  Clinic__r.Email__c, Clinic__r.Email_Preference__c FROM VaccinationRegister__c ';
String WhereClause = '(Clinic__r.Email_Preference__c = \' Monthly\'  AND Due_Date__c  =: MonthDate ) OR (Clinic__r.Email_Preference__c =\' Fortnightly\' AND Due_Date__c  =: fortdate)';

String query = SelectClause + ' WHERE ' + WhereClause ;
System.debug('query--'+query);

 
s k 85s k 85
Hi Raj,
I changed my code as you recomended still my allaccount is not getting email only one account is concidered and one that account is getting mail like my earlier code was working.