Skip to content

Commit

Permalink
Merge pull request #930 from bcgov/defect-ALR-1707.1
Browse files Browse the repository at this point in the history
ALR-1707
  • Loading branch information
GandlojuVishwantha authored Jul 2, 2024
2 parents 4a5574e + 736d43f commit 98648c0
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 148 deletions.
159 changes: 86 additions & 73 deletions src/main/default/classes/ScheduleSendEmail.cls
Original file line number Diff line number Diff line change
Expand Up @@ -10,121 +10,134 @@ public with sharing class ScheduleSendEmail implements Schedulable {
public static Set<Id> accountIds = new Set<Id>();

public void execute(SchedulableContext sc) {
try {
try {
Map<Id, Set<Id>> accountIdToContactIdsMap = new Map<Id, Set<Id>>();

List<Account> accounts = [
List<Account> accounts = [
SELECT Id ,ParentId
FROM Account
WHERE Status__c IN ('Registered Active', 'Registered Active with Conditions', 'Registered Active Progressive Enforcement')
];

for (Account acc : accounts) {
for (Account acc : accounts) {
accountIds.add(acc.Id);
accountIds.add(acc.ParentId);
accountIdToContactIdsMap.put(acc.Id, new Set<Id>());
accountIdToContactIdsMap.put(acc.ParentId, new Set<Id>());
}

Map<Id, Account> accountIdToAccountMap = new Map<Id, Account>([
Map<Id, Account> accountIdToAccountMap = new Map<Id, Account>([
SELECT Id,parentId,
(SELECT Id, ContactId, Contact.Name, Contact.Email
FROM AccountContactRelations
WHERE IsActive = true AND Contact.Email != null)
(SELECT Id, ContactId, Contact.Name, Contact.Email
FROM AccountContactRelations
WHERE IsActive = true AND Contact.Email != null)
FROM Account
WHERE Id IN :accountIds
]);

for (Account account : [
SELECT parentId,
(SELECT Id, ContactId, Contact.Name, Contact.Email
FROM AccountContactRelations
WHERE IsActive = true AND Contact.Email!= null)
FROM Account
WHERE Id IN : accountIds
]) {
accountIdToAccountMap.put(account.Id, account);
}

SELECT parentId,
(SELECT Id, ContactId, Contact.Name, Contact.Email
FROM AccountContactRelations
WHERE IsActive = true AND Contact.Email!= null)
FROM Account
WHERE Id IN : accountIds
]) {
accountIdToAccountMap.put(account.Id, account);
}
for (Account acc : accountIdToAccountMap.values()) {
for (AccountContactRelation acr : acc.AccountContactRelations) {
accountIdToContactIdsMap.get(acc.Id).add(acr.ContactId);
}
}

List<Id> contactIdsList = new List<Id>();
for (Set<Id> contactIds : accountIdToContactIdsMap.values()) {
contactIdsList.addAll(contactIds);
}

if (!contactIdsList.isEmpty()) {
if (!contactIdsList.isEmpty()) {
sendRenewalDueEmail(contactIdsList);
}

} catch (Exception e) {
System.debug('Exception occurred in execute method: ' + e.getMessage());
}

}
}
public static void sendRenewalDueEmail(List<Id> contactIds) {
List<Id> accListforKeepingIds = new List<Id>();
try {
EmailTemplate emailTemplate = getEmailTemplate('Upcoming_Renewal_Email_Template');

OrgWideEmailAddress orgWideAddr = [
SELECT Id, DisplayName, Address
FROM OrgWideEmailAddress
WHERE DisplayName = 'ALR Support Email'
];

List<Contact> contactRec =[Select Id,Email from Contact WHERE Id IN: contactIds];

Integer batchSize = 100;
List<List<Messaging.SingleEmailMessage>> emailBatches = new List<List<Messaging.SingleEmailMessage>>();
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (Contact contact : contactRec){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(emailTemplate.Id);
email.setOrgWideEmailAddressId(orgWideAddr.Id);
email.setToAddresses(new String[] {contact.Email});
email.setTargetObjectId(contact.Id);
String emailSubject = emailTemplate.Subject;
String emailBody = emailTemplate.HtmlValue;
email.setSubject(emailSubject);
email.setHtmlBody(emailBody);
emails.add(email);
if (emails.size() == batchSize) {
emailBatches.add(emails);
emails = new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemplate = getEmailTemplate('Upcoming_Renewal_Email_Template');

OrgWideEmailAddress orgWideAddr = [
SELECT Id, DisplayName, Address
FROM OrgWideEmailAddress
WHERE DisplayName = 'ALR Support Email'
];
List<AccountContactRelation> AccountcontactRecList = [SELECT Id, AccountId, Account.Name, ContactId, Contact.Email, Contact.IsEmailBounced FROM AccountContactRelation WHERE ContactId IN :contactIds];

for(AccountContactRelation AccountconList : AccountcontactRecList){
accListforKeepingIds.add(AccountconList.ContactId);
}

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<String, List<String>> processedEmails = new Map<String, List<String>>();

for (AccountContactRelation acrList : AccountcontactRecList) {
if (acrList.contact.Email != null) {
String key = acrList.AccountId + '_' + acrList.contact.Email.toLowerCase();
if (!processedEmails.containsKey(key)) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(emailTemplate.Id);
email.setOrgWideEmailAddressId(orgWideAddr.Id);
email.setToAddresses(new String[]{acrList.contact.Email});
email.setTargetObjectId(acrList.contact.Id);
String emailBody = emailTemplate.HtmlValue;
String emailSubject = emailTemplate.Subject;
email.setSubject(emailSubject);
email.setHtmlBody(emailBody);
emails.add(email);
processedEmails.put(key, new List<String>{acrList.contact.Email});
}
}
}



if (!emails.isEmpty()) {
sendEmailBatch(emails);
}
} catch (Exception e) {
System.debug('Exception occurred in sendRenewalDueEmail method: ' + e.getMessage());
}
if (!emails.isEmpty()) {
emailBatches.add(emails);
}
for (List<Messaging.SingleEmailMessage> emailBatch : emailBatches) {
}

public static void sendEmailBatch(List<Messaging.SingleEmailMessage> emailBatch) {
for (Messaging.SingleEmailMessage email : emailBatch) {
try {
List<Messaging.SendEmailResult> results = Messaging.sendEmail(emailBatch);
for (Integer i = 0; i < results.size(); i++) {
if (results[i].isSuccess()) {
System.debug('Email sent successfully to ' + emailBatch[i].getToAddresses()[0]);
List<Messaging.SingleEmailMessage> singleEmailList = new List<Messaging.SingleEmailMessage>();
singleEmailList.add(email);
List<Messaging.SendEmailResult> results = Messaging.sendEmail(singleEmailList);

if (results[0].isSuccess()) {
System.debug('Email sent successfully to ' + email.getToAddresses()[0]);
} else {
String errorMessage = results[0].getErrors()[0].getMessage();
if (errorMessage.contains('EMAIL_ADDRESS_BOUNCED')) {
System.debug('Bounced email address: ' + email.getToAddresses()[0]);
} else {
String errorMessage = results[i].getErrors()[0].getMessage();
if (errorMessage.contains('EMAIL_ADDRESS_BOUNCED')) {
System.debug('Bounced email address: ' + emailBatch[i].getToAddresses()[0]);
} else {
System.debug('Failed to send email to ' + emailBatch[i].getToAddresses()[0] +
'. Error message: ' + errorMessage);
}
System.debug('Failed to send email to ' + email.getToAddresses()[0] +
'. Error message: ' + errorMessage);
}
}
} catch (Exception e) {
System.debug('Exception occurred in sendEmails method: ' + e.getMessage());
System.debug('Exception occurred while sending email to ' + email.getToAddresses()[0] + ': ' + e.getMessage());
}
}
}catch (Exception e) {
System.debug('Exception occurred in sendRenewalDueEmail method: ' + e.getMessage());
}
}

public static EmailTemplate getEmailTemplate(String templateDevName) {
EmailTemplate emailTemplateRec = null;
try {
Expand Down
Loading

0 comments on commit 98648c0

Please sign in to comment.