You need to sign in to do that
Don't have an account?
Akshu
* On 1st of each month send an email to Opportunity owner if close date is in this month.
*/
public class Sendmailcopy implements Database.Batchable <sobject> {
public Database.QueryLocator start(DataBase.BatchableContext BC){
return Database.getQueryLocator([select id, Name, OwnerId,Owner.Email,CloseDate from Opportunity where CloseDate=THIS_MONTH]);
}
List<String> emailList=new List<String>();
public void execute(Database.BatchableContext BC,List<Opportunity> opptyList){
for(Opportunity opp:opptyList)
{
emailList.add(opp.Owner.Email);
}
sendmail();
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(emailList);
email.setSubject(' opportunity Close Date!!!!!');
email.setPlainTextBody('Close date of this opportunity is in this month');
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
public void finish(Database.BatchableContext BC){
}
}
/*************schedule*************************
global class sendmail implements Schedulable {
global void execute(SchedulableContext ctx) {
Sendmailcopy um=new Sendmailcopy();
Database.executeBatch(um);
}
}
*/
test class for batch class to send email to opportunity
/*
* Write a batch class to send an email to Opportunity owner. * On 1st of each month send an email to Opportunity owner if close date is in this month.
*/
public class Sendmailcopy implements Database.Batchable <sobject> {
public Database.QueryLocator start(DataBase.BatchableContext BC){
return Database.getQueryLocator([select id, Name, OwnerId,Owner.Email,CloseDate from Opportunity where CloseDate=THIS_MONTH]);
}
List<String> emailList=new List<String>();
public void execute(Database.BatchableContext BC,List<Opportunity> opptyList){
for(Opportunity opp:opptyList)
{
emailList.add(opp.Owner.Email);
}
sendmail();
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(emailList);
email.setSubject(' opportunity Close Date!!!!!');
email.setPlainTextBody('Close date of this opportunity is in this month');
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
public void finish(Database.BatchableContext BC){
}
}
/*************schedule*************************
global class sendmail implements Schedulable {
global void execute(SchedulableContext ctx) {
Sendmailcopy um=new Sendmailcopy();
Database.executeBatch(um);
}
}
*/
Hi,
Please find the solution.
Please let me know it is working or not.
Please mark it as the Best Answer so that other people would take references from it.
ThankYou
Try Below Code Please Mark It As The Best Answer If It Helps
ThankYou