Birthday Reminder or Automatic Birthday wisher using apex in Saleforce

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;}

   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();
   }
}

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('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) {
   }
}

scheduleBirthdayWish:

global class scheduleBirthdayWish implements Schedulable {
    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


5 comments:

  1. Hi Magulan

    I am new to apex classes I was tried as you gave in this blog but batch apex is running with this error First error: Attempt to de-reference a null object.

    Please advise me on this..

    Thanks -Rajkumar

    ReplyDelete
  2. Use system.debug to track your code.

    Cheers!!!

    ReplyDelete
  3. Please make sure that there is no records without Email_id__c and Birthdate__c values(fields should not be null)in the member__c object. else it will throw attempt to de-reference null object error.

    ReplyDelete
  4. // this is the test class for the code i have created 2 test methods but it is not really necessary
    @istest
    public with sharing class TestBirthdayBatch {
    static testMethod void validatetestbday(){
    Account acc=new Account();
    acc.Name='GTR';
    insert acc;

    contact con = new contact(lastname='test',accountId=acc.id,email='test@test.com',birthdate=System.today(),Anniversaries__c=system.today(),anniversariesGTR__c=system.today());
    insert con;

    FeedItem fd = new FeedItem();
    fd.ParentId = '0F9P00000008QlE';
    fd.CreatedById = '0050000000789F8';
    fd.Body = 'Happy BirthDay '+con.Name+' Have a Great Year Ahead';
    insert fd;

    scheduleBirthdayWish sendEmail = new scheduleBirthdayWish ();
    String schedule = '0 0 23 * * ?';
    system.schedule('Nightly Update', schedule, sendEmail);

    MemberIterable mi=new MemberIterable();
    mi.next();
    mi.hasNext();

    callMemberIterable cmi=new callMemberIterable();
    cmi.Iterator();
    }

    ReplyDelete
    Replies
    1. but the code coverage of memberBirthdayBatch class remain 0%.Hence I am not able to move in production

      Delete