You need to sign in to do that
Don't have an account?
Send Birthday Email Using batch class
Actually I want to send the Automated birthday email notification whose birthdate is excactly equal to system. today() day and month. suppose i am having one custoom object named client and under that object there is custom field named Date Of Birth and email...and what i want is I want to schedule this every day and if the d.o.b filed data in exsisting record in client object day and month is equal to system.today() day and month then send the email to that all that records whose birthdate is today and also send email to that one more client whose email i had put it by hard coding.. i had used this logic.. but failed.. I checked on apex job it shows error email is null.... i hope you all understand... what I am trying to do..plz help
global class sendGreetingWishes implements Database.Batchable<sObject> { string query; global Database.querylocator start(Database.BatchableContext bc) { String query= 'SELECT Id, Name, email__c,birthday__c FROM stud__c'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext bc, List<stud__c> scope) { for(stud__c iteratorUser : scope) { date thisdate=system.today(); integer sysday=thisdate.day(); integer sysmonth=thisdate.month(); string dayandmonth=sysday+'/'+sysmonth; Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); List<String> toAddresses = new List<String>(); toAddresses.add(iteratorUser.email__c); email.setToAddresses(toAddresses); List<String> ccAddresses = new List<String>(); ccAddresses.add('pranavlakshya009@gmail.com'); email.setCcAddresses(ccAddresses); email.setSubject('Happy Birthday. Have a blast — Birthday Reminder!'); String message = 'Happy Birthday'; email.setHtmlBody(message); Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email}); } } global void finish(Database.batchableContext bc) { } }
if you are getting email is null error then you should filter out null emails from your query.
the process i had written above is correct???
Use below code.It will work for your requirement.
Thanks,
Gopii.
global class SampleBatchClass implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
string query ='select id,email,lastname,firstname from contact where bithday = system.today()';
//This is query for getting records
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<account> AllaccountRecords){
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
List<String> sendTo = new List<String>();
List<String> ccTo = new List<String>();
for(account acc : AllaccountRecords){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
sendTo.add(con.Email);
mail.setToAddresses(sendTo);
mail.setSenderDisplayName(' Birthday Wishes');
// (Optional) Set list of people who should be CC'ed
ccTo.add('priyaaakanksha28@gmail.com');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject(' Have a Blast Birthday ');
mails.add(mail);
}
if(mails.size() > 0){
Messaging.sendEmail(mails);
}
}
global void finish(Database.BatchableContext BC){
}
}
Getting Error Like
Variable does not exist: con.
Please help me out from this.
Very Thankful in Advance
Hi Pranav i follwed your code but i got this error can you tell me how to resolve
16:04:55:032 EXCEPTION_THROWN [26]|System.EmailException: SendEmail failed. First exception on row 0; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded.: []
[My Batch Class]
global class sendGreetingWishes implements Database.Batchable<sObject>
{
global Database.querylocator start(Database.BatchableContext bc)
{
String query= 'SELECT Id, Name, Email,Birthdate FROM Contact where Birthdate != null';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Contact> scope)
{
for(Contact Usersq : scope)
{
date thisdate=system.today();
integer sysday=thisdate.day();
integer sysmonth=thisdate.month();
string dayandmonth=sysday+'/'+sysmonth;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<String> sendTo = new List<String>();
sendTo.addAll((Label.Account_Mail_Notification).split(';'));
email.setToAddresses(sendTo);
List<String> ccAddresses = new List<String>();
ccAddresses.add('sultanyaseen612@gmail.com');
email.setCcAddresses(ccAddresses);
email.setSubject('Happy Birthday. Have a blast — Birthday Reminder!');
String message = 'Happy Birthday';
email.setHtmlBody(message);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
global void finish(Database.batchableContext bc)
{
//Finn
}
}