I want send a reminder to a contact owner for birthday reminder based on the birthday field in the contact object. If the birthday falls on week day notification has to be sent on the same else if on Saturday one day before and else if on Sunday two days before can i know how to achieve the above logic?
global class contactBirthdayBatch implements Database.batchable<contact>
{
global Iterable<contact> start(Database.batchableContext info)
{
System.debug('Start method');
return new callContactIterable();
}
global void execute(Database.batchableContext info, List<contact> scope)
{
List<contact> consToUpdate = new List<contact>();
System.debug('contact list size is ' + scope.size());
for(contact c : scope)
{
if(c.Email != null && c.Birthdate != null){
Date myDate = date.today();
Integer todayDy = myDate.day();
Integer todayMon = myDate.month();
System.debug('Day is ' + c.Birthdate.day());
Integer dy = c.Birthdate.day();
Integer mon = c.Birthdate.month();
Date d = date.newinstance(myDate.Year(),mon, dy);
Integer calvalue =caluactedate(d);
if(todayDy == dy && todayMon == mon && calvalue==0)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(c.Email);
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});
}
if(todayDy == dy-4 && todayMon == mon && calvalue==1)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(c.Email);
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});
}
if(todayDy == dy-2 && todayMon == mon && calvalue==2)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(c.Email);
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});
}
}
}
}
public integer caluactedate(date d)
{
Date startDate = date.newInstance(0001, 1, 1);
List<String> listDay = new List<String>{'Saturday' , 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday'};
Date selectedDate = d;
integer dateValue;
string dayValue;
Integer remainder = Math.mod(startDate.daysBetween(selectedDate) , 7);
dayValue = listDay.get(remainder);
if(dayValue =='Monday'|| dayValue =='Tuesday' || dayValue =='Wednesday' || dayValue =='Thursday' || dayValue =='Friday'){
dateValue = 0;
}else if( dayValue =='Saturday'){
dateValue = 1;
}else if(dayValue =='Sunday'){
dateValue = 2;
}
return dateValue;
}
Public void createTaskAndSendEmail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
toAddresses.add(c.Email);
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)
{
}
}