For Birthday Reminder or Automatic Birthday wisher using apex in Saleforce, use the below codes. I have implemented this using Batch Apex.
MemberIterable:
global class MemberIterable implements Iterator<Member__c> {
List<Member__c> memList{get; set;}
Integer i {get; set;}
List<Member__c> memList{get; set;}
Integer i {get; set;}
public MemberIterable() {
memList = [SELECT E_Mail_Id__c,Birthday__c FROM Member__c];
i = 0;
}
global boolean hasNext() {
if(i >= memList.size())
{
return false;
} else {
return true;
}
}
global Member__c next() {
if(i > memList.size()) {
return null;
}
i++;
return memList[i-1];
}
}
callMemberIterable:
global class callMemberIterable implements iterable<Member__c> {
global Iterator<Member__c> Iterator() {
return new memberIterable();
}
}
global Iterator<Member__c> Iterator() {
return new memberIterable();
}
}
memberBirthdayBatch:
global class memberBirthdayBatch implements Database.batchable<Member__c> {
global Iterable<Member__c> start(Database.batchableContext info) {
System.debug(‘Start method’);
return new callMemberIterable();
}
global void execute(Database.batchableContext info, List<Member__c> scope) {
List<Member__c> memsToUpdate = new List<Member__c>();
System.debug(‘Member list size is ‘ + scope.size());
for(Member__c m : scope) {
Date myDate = date.today();
Integer todayDy = myDate.day();
Integer todayMon = myDate.month();
System.debug(‘Day is ‘ + m.Birthday__c.day());
Integer dy = m.Birthday__c.day();
Integer mon = m.Birthday__c.month();
if(todayDy == dy && todayMon == mon) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(m.E_Mail_Id__c);
email.setToAddresses(toAddresses);
List<String> ccAddresses = new List<String>();
ccAddresses.add(‘[email protected]’);
email.setCcAddresses(ccAddresses);
email.setSubject(‘Happy Birthday. Have a blast — Birthday Reminder!’);
String message = ‘<html><table cellspacing = “7”><tr><td style=”font-weight:bold;color:green;”>Happy Birthday!!!</td></tr><tr><td style=”font-weight:bold;color:pink;”>Many more Happy returns of the day.</td></tr><tr><td></td></tr><tr><td></td></tr><tr><td style=”font-weight:bold;”>Cheers,</td></tr><tr><td style=”font-weight:bold;”>Magulan D</td></tr></table></html>’;
email.setHtmlBody(message);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
}
global void finish(Database.batchableContext info) {
}
}
global Iterable<Member__c> start(Database.batchableContext info) {
System.debug(‘Start method’);
return new callMemberIterable();
}
global void execute(Database.batchableContext info, List<Member__c> scope) {
List<Member__c> memsToUpdate = new List<Member__c>();
System.debug(‘Member list size is ‘ + scope.size());
for(Member__c m : scope) {
Date myDate = date.today();
Integer todayDy = myDate.day();
Integer todayMon = myDate.month();
System.debug(‘Day is ‘ + m.Birthday__c.day());
Integer dy = m.Birthday__c.day();
Integer mon = m.Birthday__c.month();
if(todayDy == dy && todayMon == mon) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(m.E_Mail_Id__c);
email.setToAddresses(toAddresses);
List<String> ccAddresses = new List<String>();
ccAddresses.add(‘[email protected]’);
email.setCcAddresses(ccAddresses);
email.setSubject(‘Happy Birthday. Have a blast — Birthday Reminder!’);
String message = ‘<html><table cellspacing = “7”><tr><td style=”font-weight:bold;color:green;”>Happy Birthday!!!</td></tr><tr><td style=”font-weight:bold;color:pink;”>Many more Happy returns of the day.</td></tr><tr><td></td></tr><tr><td></td></tr><tr><td style=”font-weight:bold;”>Cheers,</td></tr><tr><td style=”font-weight:bold;”>Magulan D</td></tr></table></html>’;
email.setHtmlBody(message);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
}
global void finish(Database.batchableContext info) {
}
}
scheduleBirthdayWish:
global class scheduleBirthdayWish implements Schedulable {
global void execute(SchedulableContext SC) {
memberBirthdayBatch mbw = new memberBirthdayBatch();
Database.executeBatch(mbw);
}
}
global void execute(SchedulableContext SC) {
memberBirthdayBatch mbw = new memberBirthdayBatch();
Database.executeBatch(mbw);
}
}
Schedule the ‘scheduleBirthdayWish’ as mentioned below in App Setup –> Apex Classes –> Schedule Apex and select ‘scheduleBirthdayWish’ class. Check Weekly and select all the days. Give Start and End dates and Preferred Start time and Click ‘Save’ button.
To check the scheduled job, go to Administration Setup –> Monitoring –> Scheduled Jobs