Simple Batch class for Birthday Notification

Sample Batch class:

global class BirthdayNotificationBatch Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        Date tody = system.today();
        String SOQL = 'SELECT Id, Name, Email__c FROM Employee__c WHERE DAY_IN_MONTH(Date_of_Birth__c) = ' + 
                      tody.day() +  ' AND CALENDAR_MONTH(Date_of_Birth__c) = ' + tody.month();
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<Employee__c> listEmployee) {
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(Employee__c m : listEmployee) {
            List<String> toAddresses = new List<String>{m.Email__c};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Happy Birthday');
            String messageBody = '<html><body>Hi ' + m.Name + ',<br/>Happy Birthday.<br/>Many More Happy '+
                                 'Returns of the day.<br/><br/><b>Regards,</b><br/>Admin</body></html>';
            mail.setHtmlBody(messageBody); 
            mailList.add(mail);          
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}

Output:


Cheers!!!

No comments:

Post a Comment