+ Start a Discussion
SFDC_Dev_2009SFDC_Dev_2009 

Birthday reminder

I am creating a workflowto assign a task for the contact owner before 7 days on contact's birthday.

I have created a formula field to calculate contacts next birthdate from Birthdate field using the following formula

 

IF(MONTH(Birthdate)>MONTH(TODAY()),DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)),

IF(MONTH(Birthdate)<MONTH(TODAY()),DATE(YEAR(TODAY())+1,MONTH(Birthdate),DAY(Birthdate)),

IF(DAY(Birthdate) >=

(DAY(TODAY())),DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)),

DATE(YEAR(TODAY())+1,MONTH(Birthdate),DAY(Birthdate)))))

 

 

I have assigned a task,now how do i test this?

Nirmal ChristopherNirmal Christopher

check the code in my next post it is 100% working

Nirmal ChristopherNirmal Christopher

global class memberBirthdayBatch implements Database.batchable<contact>
{
   global Iterable<contact> start(Database.batchableContext info)
   {
       System.debug('Start method');
       return new callMemberIterable();     
   }
   global void execute(Database.batchableContext info, List<contact> scope)
   {
       List<contact> memsToUpdate = new List<contact>();
       System.debug('++++++++++++++++++++++Member list size is++++++++++++++++++++++++++ ' + scope.size());
       for(contact m : scope)
       {
           Date myDate = date.today();
           Integer todayDy = myDate.day();
           Integer todayMon = myDate.month();
            
           Integer dy =  m.Birthdate.day();
          
           Integer mon =  m.Birthdate.month();
                      System.debug('++++++++++++++++++++++++++Day is ++++++++++++++++++++++++++' + m.Birthdate.day());

           if(todayDy == dy && todayMon == mon)
           {
               Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
               List<String> toAddresses = new List<String>();
               toAddresses.add(m.email);
               email.setToAddresses(toAddresses);
               List<String> ccAddresses = new List<String>();
               ccAddresses.add('magulancse24@gmail.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)
   {
   }
}


Nirmal Christopher wrote:

      We cannot create birthday reminder using workflow because of the workflow conditions we give ...work  flow will fire only when the record is edited or inserted or when ever it is subsequently meeting the criteria.so user cannot update the record everytime inorder to fire it.

     You can also try creating time based workflow still it wont work for leap years. But there is a solution for this unless your org is not professional edition. You can try creating a apex scheduler class and create  scheduled job. The class will execute whenever time criteria meets. To test it you can run the anonymous code in developer console and check. I'l  post the code it will help you if u r working in Apex environment. check this code if the contact birthday is met todays date mail will trigger.its tested and 100% working.-----------------------

----------------

global class MemberIterable implements Iterator<contact>
{
   List<contact> memList{get; set;}
   Integer i {get; set;}

   public MemberIterable()
   {
       memList = [SELECT EMail,Birthdate FROM contact];
       i = 0;
   }

   global boolean hasNext()
   {
       if(i >= memList.size())
       {
           return false;
       } else {
           return true;
       }
   }

   global contact next()
   {
       if(i > memList.size())
       {
           return null;
       }
       i++;
       return memList[i-1];
   }
}

----------------------

 

 

-------------------

global class callMemberIterable implements iterable<contact>
{
   global Iterator<contact> Iterator()
   {
      return new memberIterable();
   }
}
global class scheduleBirthdayWish implements Schedulable
{
    global void execute(SchedulableContext SC)
    {
        memberBirthdayBatch mbw = new memberBirthdayBatch();
        Database.executeBatch(mbw);
    }
}

Ramakrishnan AyyanarRamakrishnan Ayyanar
Problems:

1)If DOB is Feb 29 Leap year.How to find next birthday for that particular date?

No Need Validation rule for this code.

End-of-century years 1700, 1800,1900,2100 these years are not leap year in these problems are resolved.........

To find the next Birthday formula

How to create......

step-1: create the custom field for Date of Birth----It's type-Date

step-2: create the formula field for Birthday ---It's type-Date

code:

iIF ( MONTH (ramki__DOB__c)=2 && DAY (ramki__DOB__c)=29,
DATE(
IF(Year(Today())<=Year(ramki__DOB__c),Year(ramki__DOB__c),
IF(
OR(MOD(Year(Today())+ 4-MOD(Year(Today()),4),400)=0,
AND(MOD(Year(Today())+ 4-MOD(Year(Today()),4),4)=0,
MOD(Year(Today())+ 4-MOD(Year(Today()),4),100)<>0)),
Year(Today())+ 4-MOD(Year(Today()),4),
Year(Today())+ 8-MOD(Year(Today()),4))
),
MONTH(ramki__DOB__c),DAY(ramki__DOB__c)),
IF (DATE (YEAR(TODAY()),MONTH(ramki__DOB__c),DAY(ramki__DOB__c)) > TODAY(),
DATE (YEAR(TODAY()),MONTH(ramki__DOB__c),DAY(ramki__DOB__c)),
DATE (YEAR(TODAY())+1,MONTH(ramki__DOB__c),DAY(ramki__DOB__c))
)
)

it's correctly worked for leap year dates.......
this is code for find the next Birthday friends......


Easily Calculate Age:

create formula field -------(Number)

Age=YEAR( ramki__BirthDay__c) - YEAR(ramki__DOB__c)
----
softramki@gmail.com
+919944112175