Create a schedulable batch class as below:
Apex class:
global class NotificationEmailtoAccountExecutive implements Database.Batchable < sObject >, Schedulable, Database.Stateful
{
global List<String> errorMessages = new List<String>();
global Database.QueryLocator start(Database.BatchableContext bc)
{
Date ed = Date.today().addDays(2); // add two days for sending mail before two days
System.debug('date after add two days: '+ed);
string query = 'SELECT id,name,(select id from contacts), Agrrement_Termination__c FROM account where Agrrement_Termination__c =: ed';
system.debug('my Query is '+query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List < account > recs) {
List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();
for (account a: recs) {
if (a.Agrrement_Termination__c != null) {
List < String > toAddresses = new List < String > ();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
toAddresses.add('test@test.com');// to Acc Executive mail/or manager mail
mail.setToAddresses(toAddresses);
id tempId = [select id from emailTemplate where name='Termination popup' limit 1].id;
mail.setTemplateId(tempId); // set Tempplate id
mail.setSaveAsActivity(false);
mail.setWhatId(a.id);
mail.setTargetObjectId('0036F00003gR4TUQA0');// not send just for using the template id because emailTemplat need this
mail.setTreatTargetObjectAsRecipient(false);
mail.setWhatId(a.id);
mailList.add(mail);
}
}
Messaging.sendEmail(mailList);
}
global void finish(Database.BatchableContext bc)
{
system.debug('Finish method call');
}
global void execute(SchedulableContext SC) {
NotificationEmailtoAccountExecutive batchable = new NotificationEmailtoAccountExecutive();
database.executebatch(batchable);
}
}
=================================================================
Schedule this method according to your choice.
REF:ak@infoglen.com
No comments:
Post a Comment