You need to sign in to do that
Don't have an account?
THUNDER 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?
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
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);
}
}