• s k 85
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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) {
    }
}
 
  • September 28, 2018
  • Like
  • 0
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) {
    }
}
 
  • September 28, 2018
  • Like
  • 0