+ Start a Discussion
Naveen DhanarajNaveen Dhanaraj 

I just wanted to send the birthday wishes automatically who are in the patient list and in Doctors list

I have a two ojects called Patients and Doctors .In that i have field called DOB(date type ).it should send email of birthday wishes to all the records in patients and doctors AUTOMATICALLY on their Birthday
Nachu RY 4Nachu RY 4
Hi Naveen ,

You have to write  batch class .
In workflow we can't do this becase we have to fire the email for the saved record .Even Timebased workflow will fire only once , user will not get email for his/her next birthday.

Thanks.
Naveen DhanarajNaveen Dhanaraj
Hi,Nachu RY 4
        can you help me regarding how to write a batch class for this
JyothsnaJyothsna (Salesforce Developers) 
Hi Naveen,

You can achieve this functionality by using Batch Apex and Schedule Apex.
Please check the below sample code.

Batch Apex:
 
global class BatchWishes implements Database.Batchable<Employee__c>{
     global Iterable<employee__c> start(Database.BatchableContext bc){
     list<employee__c> sq=[select name,empname__c ,Email_Id__c from employee__c where Date_of_Birth__c=today];
    return sq;
    }
   global  void execute(Database.BatchableContext bc, List<employee__c> lst)
      {
list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();

        for(employee__c e: lst)
        {
         Messaging.SingleEmailMessage maileach = new Messaging.SingleEmailMessage();
         list<string> toadd=new list<string>();
         toadd.add(e.Email_Id__c );
          maileach.settoaddresses(toadd);
          maileach.setsubject('birthday wishes');
          maileach.setplaintextbody('happy birthday '+e.empname__c);
          mail.add(maileach);
        }
      
    Messaging.SendEmailResult[]  result  =Messaging.sendEmail( mail);
     
    } 
   global  void finish(Database.BatchableContext sc) 
   
    {
     
    }
   
}

Schedule Apex:
 
global class Batchwishes_Scheduled Implements Schedulable
    {
      global   void execute(SchedulableContext dc){
        // Create instance for Batch class  
         BatchWishes bth=new BatchWishes ();
         Database.executeBatch(bth,20);
    }
}
Scheduling an Apex Job through the ui

Go to Setup -> Develop -> Classes--->You will see a "Schedule Apex" button. You can set up the timing from there.

Hope this helps you!
Best Regards,
Jyothsna
Srinivas SSrinivas S
Hi Naveen,

As Jyothsna specified you can follow, I am extending batch class functionality to support multiple objects, pelase find the following solution -

Batch Class:
global class BirthDayWishesNotifier implements Database.Batchable<Sobject>, Database.Stateful {
	String query = '';
	String dateField;
	List<Messaging.SingleEmailMessage> mails;
	global BirthDayWishesNotifier(String ObjectName, String dateFieldName) {
		dateField = dateFieldName;
		query = 'select '+dateFieldName+' from '+ObjectName;
	}
	global Database.QueryLocator start(Database.BatchableContext bc){
		return Database.getQueryLocator(query);
	}
	global  void execute(Database.BatchableContext bc, List<Sobject> sobjLst) {
		mails = new List<Messaging.SingleEmailMessage>();
        for(Sobject sobj : sobjLst) {
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			mail.settoAddresses(new List<String>{sobj.get(dateField)});
			mail.setSubject('Happy Birthday!!!');
			mails.add(mail);
        }		
    } 
	global  void finish(Database.BatchableContext sc) {
		//Post commit logic
		if(mails.size() > 0) {
			//Send emails in finish method to avoid some governor limits.
			Messaging.sendEmail(mails);
		}	
	}   
}

Schedulable Class:
global class BirthDayWishesNotifierSchedular implements Schedulable {
   global void execute(SchedulableContext SC) {
      BirthDayWishesNotifier patientSchedule = new BirthDayWishesNotifier('Patient__c','DOB__C'); 
	  BirthDayWishesNotifier doctorSchedule = new BirthDayWishesNotifier('Doctor__c','Birthdate__C');
	  Database.executeBatch(patientSchedule);
	  Database.executeBatch(doctorSchedule);
   }
}


Schedule:
Navigation: Setup > Develop > Apex Classes > Click on 'Schedule Apex'

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
Pathan S 4Pathan S 4
You don't have permission to see any of the records.
how to see apex class.how to enable n select apex calsees when i create a shedule apex.plz reply