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.
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.
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()];
You can achieve the above scenario by using Batch Apex and Schedule Apex. Please check the below sample code.
Batch Class
Schedule class
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.
Hope this helps you!
Best Regards,
Jyothsna
All Answers
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) {
}
}
You can achieve the above scenario by using Batch Apex and Schedule Apex. Please check the below sample code.
Batch Class
Schedule class
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.
Hope this helps you!
Best Regards,
Jyothsna
Javeed Shaik
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);
}
}