(Lead Status - Open) If the lead is still in the same condition(open) after 5 hours of lead creation, an email has to be sent to the owner of the lead and the manager of the owner in email CC.
I have created batch class like this but it is sending email to owner not manager. I want both.
public class OpenLeadEmailBatch implements Database.Batchable<SObject>, Database.AllowsCallouts {
public Database.QueryLocator start(Database.BatchableContext context) {
// Query for leads with status Open created 5 hours ago or earlier
DateTime cutoffTime = DateTime.now().addHours(-5);
String query = 'SELECT Id, OwnerId, Owner.Email ,Owner.Manager.Email FROM Lead WHERE Status__c = \'Open\' AND CreatedDate <= :cutoffTime';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext context, List<Lead> scope) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (Lead lead : scope) {
// Create email for lead owner
Messaging.SingleEmailMessage emailToOwner = new Messaging.SingleEmailMessage();
emailToOwner.setToAddresses(new String[]{ lead.Owner.Email });
// emailToOwner.setCCAddresses(new String[]{ lead.Owner.Manager.Email }); //set the manager email id field
emailToOwner.setSubject('Open Lead Reminder');
emailToOwner.setPlainTextBody('This is a reminder that the lead with ID: ' + lead.Id + ' is still open.');
emails.add(emailToOwner);
}
// Send the emails
if (!emails.isEmpty()) {
Messaging.sendEmail(emails);
}
}
public void finish(Database.BatchableContext context) {
}
}
(Lead Status - Open) If the lead is still in the same condition(open) after 5 hours of lead creation, an email has to be sent to the owner of the lead and the manager of the owner in email CC.
I have created batch class like this but it is sending email to owner not manager. I want both.