function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
THUNDER CLOUDTHUNDER CLOUD 

How to send email on birthday ?

My org has 10000 employees and I want to send email on their birthday. How to automate this process?
Best Answer chosen by THUNDER CLOUD
JyothsnaJyothsna (Salesforce Developers) 
Hi,

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

Batch Class
 
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) 
   
    {
      system.debug(sc);
    
    }
}

Schedule class
 
global class BirthdayNameOptions implements Schedulable{

global   void execute(SchedulableContext dc){
//batch class name   
 BatchWishes bth=new BatchWishes ();
    Database.executeBatch(bth,20);
    }
}

You can schedule a batch which runs at 12 am every day.Please follow the below steps for Schedule a batch class.

Go to Setup--->Develop--->Apex classes--->Schedule Apex

Here you can set the time.Please check the below screenshot.

User-added image


Hope this helps you!
Best Regards,
Jyothsna
 

All Answers

Nayana KNayana K
You can schedule a batch which runs at 12am everyday.In batch class fetche employees data where bday = today and write logic for sending an email.
ManojSankaranManojSankaran
Hi Thunder Cloud !!!!

Below is the code that you can use for sending email on ur employee bithday. As mentioned by Nayana K schedule it every day.

Mark it as Best Answer if it really help you.

Thanks
Manoj S

global class BirthdayNotificationBatch Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        Date tody = system.today();
        String SOQL = 'SELECT Id, Name, Email__c FROM Employee__c WHERE DAY_IN_MONTH(Date_of_Birth__c) = ' + 
                      tody.day() +  ' AND CALENDAR_MONTH(Date_of_Birth__c) = ' + tody.month();
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<Employee__c> listEmployee) {
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(Employee__c m : listEmployee) {
            List<String> toAddresses = new List<String>{m.Email__c};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Happy Birthday');
            String messageBody = '<html><body>Hi ' + m.Name + ',<br/>Happy Birthday.<br/>Many More Happy '+
                                 'Returns of the day.<br/><br/><b>Regards,</b><br/>Admin</body></html>';
            mail.setHtmlBody(messageBody); 
            mailList.add(mail);          
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}
JyothsnaJyothsna (Salesforce Developers) 
Hi,

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

Batch Class
 
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) 
   
    {
      system.debug(sc);
    
    }
}

Schedule class
 
global class BirthdayNameOptions implements Schedulable{

global   void execute(SchedulableContext dc){
//batch class name   
 BatchWishes bth=new BatchWishes ();
    Database.executeBatch(bth,20);
    }
}

You can schedule a batch which runs at 12 am every day.Please follow the below steps for Schedule a batch class.

Go to Setup--->Develop--->Apex classes--->Schedule Apex

Here you can set the time.Please check the below screenshot.

User-added image


Hope this helps you!
Best Regards,
Jyothsna
 
This was selected as the best answer
Javeed ShaikJaveed Shaik
Hi Jyosthna,
list<employee__c> sq=[select name,empname__c ,Email_Id__c from employee__c where Date_of_Birth__c=today];
In the above query 'Date_of_Birth__c' can never be equal to 'taday' as it is a past date.
list<employee__c> sq=[select name,empname__c ,Email_Id__c from employee__c where Date_of_Birth__c.day()=date.today.day() AND Date_of_Birth__c.month()=date.today.month()];
Thanks,
Javeed Shaik
 
Iltima IzharIltima Izhar
Hi,

Below is the code that you can use for sending email on your employee bithday and schedule it every day as mentioned by Nayana K .


global class WishOnBirthdayBatch implements Database.Batchable<sObject> , schedulable
{
    global void execute(SchedulableContext ctx) 
    {
      WishOnBirthdayBatch batch = new WishOnBirthdayBatch();
      Database.executeBatch(batch,20);
    }
    global Database.QueryLocator start(Database.BatchableContext bc)
    {    
        Integer day= date.today().day();
        Integer mon = date.today().month();
        String EmpList= 'select name, Employees_Name__c , Wish_me_on__c,Email__c from Company__c where DAY_IN_MONTH(Wish_me_on__c) = : day AND CALENDAR_MONTH(Wish_me_on__c) = : mon limit 100';
        Return Database.getQueryLocator(EmpList);
    }
    global  void execute(Database.BatchableContext bc, List<Company__c> lst)
    {
        list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();

        for(Company__c comp: lst)
        {
            Messaging.SingleEmailMessage mailToEmployees = new Messaging.SingleEmailMessage();
            list<string> toadd=new list<string>();
            toadd.add(comp.Email__c );
            mailToEmployees.settoaddresses(toadd);
            mailToEmployees.setsubject('birthday wishes');
            mailToEmployees.setplaintextbody('happy birthday '+comp.Employees_Name__c);
            mail.add(mailToEmployees);
        }
      
        Messaging.SendEmailResult[]  result  = Messaging.sendEmail( mail);
         System.debug(result);
    } 
    global  void finish(Database.BatchableContext sc)
    {
        system.debug(sc);
    }
}