You need to sign in to do that
Don't have an account?

send a mass email using batch apex.
As I am new to salesforce , can you please help me out in writing batch apex?
Our requirement is to send a mass email to all EventUsers(child of contact) at specified interval of time.
Please reply ASAP as it is an urgent requirement.
Hi,
You can do this by creating a scheduler Class, Here you can get all the email address through Query and then send the mass email.
Try the below code snippet as reference:
global class TimeRecordReminder implements Schedulable
{
public List<CustomUser__c> usr{get;set;} //
public List<String> usr_email{get;set;}
public Map<string,string> Map_usr{get;set;}
global void execute(SchedulableContext ctx)
{
// TODO: your code here
usr=new List<CustomUser__c>();
usr_email=new List<String>();
usr.clear();
usr_email.clear();
usr=[select id,Email__c,Latest_Date__c from CustomUser__c where Latest_Date__c!=:system.today()];
Map_usr=new Map<string,string>();
for(CustomUser__c u:usr)
{
system.debug('_____________id______'+u.id+'___________Email______________'+u.Email__c+'_________DATE______________________________________'+u.Latest_Date__c);
Map_usr.put(u.id,u.Email__c);
if(u.Email__c!=null)
usr_email.add(u.Email__c);
}
if(usr_email.size()>0)
sendmail(usr_email);
}
public void sendmail(List<String> str)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress=str;
String[] ccAddresses=new String[]{'pbhat@nav.com'};
email.setSubject('Mass Email');
email.setHtmlBody('Dear Sir /Madam,<br/><br/> Hello <br/><br/>Sincerely,<br><br/><br/>Date :'+string.valueof(system.today()));
email.setToAddresses(toaddress);
email.setCcAddresses(ccAddresses);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}
Now you can schedule this class by two ways
1. system.schedule('SCheduled Job',sch,rrAcc); //Method To Schedule
OR
2. If you need to schedule on weekly/Monthly basis then you can Go To App Setup -> Develop -> Apex Classes->click On "Schedule Apex" Button
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Hi,
You can do this by creating a scheduler Class, Here you can get all the email address through Query and then send the mass email.
Try the below code snippet as reference:
global class TimeRecordReminder implements Schedulable
{
public List<CustomUser__c> usr{get;set;} //
public List<String> usr_email{get;set;}
public Map<string,string> Map_usr{get;set;}
global void execute(SchedulableContext ctx)
{
// TODO: your code here
usr=new List<CustomUser__c>();
usr_email=new List<String>();
usr.clear();
usr_email.clear();
usr=[select id,Email__c,Latest_Date__c from CustomUser__c where Latest_Date__c!=:system.today()];
Map_usr=new Map<string,string>();
for(CustomUser__c u:usr)
{
system.debug('_____________id______'+u.id+'___________Email______________'+u.Email__c+'_________DATE______________________________________'+u.Latest_Date__c);
Map_usr.put(u.id,u.Email__c);
if(u.Email__c!=null)
usr_email.add(u.Email__c);
}
if(usr_email.size()>0)
sendmail(usr_email);
}
public void sendmail(List<String> str)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress=str;
String[] ccAddresses=new String[]{'pbhat@nav.com'};
email.setSubject('Mass Email');
email.setHtmlBody('Dear Sir /Madam,<br/><br/> Hello <br/><br/>Sincerely,<br><br/><br/>Date :'+string.valueof(system.today()));
email.setToAddresses(toaddress);
email.setCcAddresses(ccAddresses);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}
Now you can schedule this class by two ways
1. system.schedule('SCheduled Job',sch,rrAcc); //Method To Schedule
OR
2. If you need to schedule on weekly/Monthly basis then you can Go To App Setup -> Develop -> Apex Classes->click On "Schedule Apex" Button
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi ,
I have one more problem to share,
Suppose I want to send an email to all contacts which are belongs to a single account.
I want to get Account ID in batch apex dynamically.
How can I get Account ID?
Dear Navatar, thanks so much for your code. It was very usefully.
hi,
this is my code
i want to send bulk emails what to so?
public class Weekly_Plan
{
public Weekly_Plan(ApexPages.StandardController controller)
{
}
/*public String LoggedInUSer = UserInfo.getUserName();
public String getUserName()
{
return UserInfo.getFirstName()+' '+UserInfo.getLastName();
}*/
public Weekly_Plan__c WeeklyPlan = new Weekly_Plan__c();
public Weekly_Plan__c getuser()
{
return WeeklyPlan;
}
public void setuser(String name)
{
WeeklyPlan.Assinged_To__c = name;
}
public Weekly_Plan__c getTodate()
{
return WeeklyPlan;
}
public void setTodate(Date d)
{
WeeklyPlan.To_Date__c = d;
}
public Weekly_Plan__c getFromdate()
{
return WeeklyPlan;
}
public void setFromdate(Date d)
{
WeeklyPlan.From_Date__c = d;
}
public Plan_Info__c PlanInfo = new Plan_Info__c();
public Plan_Info__c getLocation()
{
return PlanInfo;
}
public void setLocation(String s)
{
PlanInfo.Location__c = s;
}
public Plan_Info__c getDescription()
{
return PlanInfo;
}
public void setDescription(String s)
{
PlanInfo.description__c = s;
}
public Plan_Info__c getPlanDate()
{
return PlanInfo;
}
public void setPlanDate(Date d)
{
PlanInfo.Date__c = d;
}
public List<InnerPlans> PlanInfoList = new List<InnerPlans>();
public Integer i = 0;
public void AddPlans()
{
if(PlanInfo.Location__c==null || PlanInfo.Description__c==null|| PlanInfo.Date__c==null)
{
ApexPages.Message msgAddr = new ApexPages.Message(ApexPages.severity.Error,'Please Enter the Required Fields');
ApexPages.addMessage(msgAddr);
}
else
{
InnerPlans InnerRef = new InnerPlans();
InnerRef.plans = PlanInfo;
InnerRef.UniqueId = i;
PlanInfoList.add(InnerRef);
i++;
PlanInfo = new Plan_Info__c();
}
}
public List<InnerPlans> getPlanInfoList()
{
return PlanInfoList;
}
public class InnerPlans
{
public Plan_Info__c plans{get;set;}
public Integer UniqueId{get;set;}
}
public boolean getDisplayPlans()
{
if(PlanInfoList.size()>0)
{
return true;
}
else
{
return false;
}
}
public void DeletePlan()
{
Integer k = 0;
boolean b = true;
for(InnerPlans IP : PlanInfoList)
{
if(IP.UniqueId==Integer.valueOf(System.CurrentPageReference().getParameters().get('DelRec')))
{
b=false;
break;
}
k++;
}
if(!b)
{
PlanInfoList.remove(k);
}
}
public Weekly_Plan()
{
SendEmail();
}
public PageReference Submit()
{
if(WeeklyPlan.To_Date__c==null || WeeklyPlan.From_Date__c==null)
{
ApexPages.Message msgAddr = new ApexPages.Message(ApexPages.severity.Error,'Please Enter the Required Fields');
ApexPages.addMessage(msgAddr);
return null;
}
else
{
//WeeklyPlan.Assinged_To__c = UserInfo.getUserId();
insert WeeklyPlan;
List<Plan_Info__c> insertPlanInfo = new List<Plan_Info__c>();
List<Task> TaskList = new List<Task>();
for(InnerPlans IPs : PlanInfoList)
{
Task t=new Task();
IPs.Plans.Weekly_Plan__c = WeeklyPlan.id;
insertPlanInfo.add(IPs.Plans);
t.OwnerId=WeeklyPlan.Assinged_To__c;
t.to_date__c=WeeklyPlan.To_Date__c;
t.From_date__c=WeeklyPlan.From_Date__c;
t.Description=IPs.Plans.Description__c;
t.Subject=IPs.Plans.Location__c;
t.ActivityDate=IPs.Plans.Date__c;
TaskList.add(t);
}
insert insertPlanInfo;
insert TaskList;
Weekly_Plan wp=new Weekly_Plan();
/*
insert t;
*/
return new PageReference('/apex/WeeklyPlanView_VF?Id='+WeeklyPlan.id);
}
}
public void SendEmail()
{
String assignedTo = '';
Date date_details=null;
String subject='';
String description='';
String Email_deet='';
String Name_det='';
List<Plan_Info__c> detail_weeklyplan=[select Date__c,Description__c,Location__c,Weekly_Plan__r.Assinged_To__c from Plan_Info__c where Weekly_Plan__c =: WeeklyPlan.id limit 1];
System.debug('iii'+detail_weeklyplan);
for(Plan_Info__c p : detail_weeklyplan)
{
assignedTo=p.Weekly_Plan__r.Assinged_To__c;
System.debug('uuu'+assignedTo);
date_details=p.Date__c;
subject=p.Location__c;
description=p.Description__c;
List<User> user_Details=[select Name,Email from User where id =: assignedTo];
System.debug('idid'+user_Details);
for(User u : user_Details)
{
Name_det=u.Name;
Email_deet=u.Email;
System.debug('eeee'+Email_deet);
if(Email_deet!=null)
{
Messaging.SingleEmailMessage emaill = new Messaging.SingleEmailMessage();
string [] toaddress= New string[]{Email_deet};
emaill.setSubject('Weekly Plan Info');
emaill.setHtmlBody('<html>' + '<h4>'+'Hello ' +Name_det+ ','+'</h4>' + '<br/>' + 'Weekly Plan Information'+','+ '<br/>'+'<br/>' +'<body align="center">'+'<table border="1" align="center">' + '<th><u>Date</u></th>' + '<th><u>Subject</u></th>' +'<th><u>description</u></th>'+ ' <tr align="center">'+'<td>'+date_details+'</td>'+'<td>'+subject+'</td>'+'<td>'+description+'</td>'+'</tr>' + '</table>' + '</body>' + '</html>');
emaill.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{emaill});
}
}
}
}
public PageReference Cancel()
{
return new PageReference('/a0E/o');
}
}
I have created an email template and I want to send the monthly email in this email template.How can I do that?
we have limit of 10 with the Messaging.SingleEmailMessage... what if the methods get called for more than 10 times in the batch class ??
With MassMailer’s Email Wizard, you don’t have to be an HTML expert to create stunning, professional emails – in fact, you don’t need to know how to code at all. Our Email Wizard is simple to use and has a Salesforce-friendly interface that you will feel comfortable using right out of the gate. Create your emails, send them immediately, or schedule them to launch at a later date and time. With MassMailer, you have the power.
You can play with this app free trail for 15 days - https://goo.gl/wvcZ3P
Do you want to know more details click here https://goo.gl/e0RtSm
Regards
Venu E