4

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('deepthi.toranala@appshark.com');
               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('deepthi.toranala@appshark.com');
               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('deepthi.toranala@appshark.com');
               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('deepthi.toranala@appshark.com');
               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)
   {
   }
}
  • Can you show what you've tried or thought of so far? There's some great questions on SFSE regarding finding out what day of the week it is. – Matt Lacey Oct 28 '14 at 9:09
  • My logic fails when 1st is on sat and sunday – user6833 Oct 28 '14 at 10:47
8

It's tricky, but you can accomplish this declaratively as well.

What you would do is create a formula field (or workflow field update) using the following formula:

CASE(
    MOD((DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))) - DATE(1900, 1, 7), 7)
    ,0,(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)))-2
    ,6,(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)))-1
    ,(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)))
)

Then using time based workflow, you'd kick off an email alert on 0 days before / after the formula field.

The formula above constructs a birthdate relative to "this year" using the Today function. In addition to that, it uses MOD magic to calculate the week day (0=Sunday .... 6=Saturday). The CASE statement will subtract the right number of days from Saturday or Sunday. If the birthdate falls on a weekday, then the default condition is met and the email will be sent out on the actual birthday.

2

You can use this code to determine what day of the week today:

Datetime dt = DateTime.now();
String dayOfWeek = dt.format('EEEE'); // it will return Monday, Tuesday etc.

This examples shows today's date, but you can use it for any date to get day of the week.

  • when 1st is Saturday the month changes so comparing with the current month is not possible – user6833 Oct 29 '14 at 4:32
0

Instead of comparing the only day and month if I compare with whole date if would be possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.