From e2b7c3a7a10b0bd431bdd50ffe7ad2878b42efa0 Mon Sep 17 00:00:00 2001 From: Kipa24 Date: Mon, 24 Jun 2024 17:01:46 +0530 Subject: [PATCH] ALR-192 --- ...sk_Created_on_Account_Record.flow-meta.xml | 170 ++++++++++++++++++ .../default/classes/ScheduleSendEmail.cls | 109 +++++++++++ .../classes/ScheduleSendEmail.cls-meta.xml | 5 + .../default/classes/ScheduledEmailTrigger.cls | 116 ++++++++++++ .../ScheduledEmailTrigger.cls-meta.xml | 5 + 5 files changed, 405 insertions(+) create mode 100644 Archive/ALR/Flows/Task_Created_on_Account_Record.flow-meta.xml create mode 100644 src/main/default/classes/ScheduleSendEmail.cls create mode 100644 src/main/default/classes/ScheduleSendEmail.cls-meta.xml create mode 100644 src/main/default/classes/ScheduledEmailTrigger.cls create mode 100644 src/main/default/classes/ScheduledEmailTrigger.cls-meta.xml diff --git a/Archive/ALR/Flows/Task_Created_on_Account_Record.flow-meta.xml b/Archive/ALR/Flows/Task_Created_on_Account_Record.flow-meta.xml new file mode 100644 index 000000000..c3b8744b1 --- /dev/null +++ b/Archive/ALR/Flows/Task_Created_on_Account_Record.flow-meta.xml @@ -0,0 +1,170 @@ + + + 61.0 + + CheckTodayDate + + 182 + 252 + Default Outcome + + checksforMarch15 + and + + Checks + EqualTo + + true + + + + GroupDetails + + + + + Default + + Checks + Boolean + AND( + MONTH(TODAY()) = 6, + DAY(TODAY()) = 23 +) + + + DueDate + Date + DATE(YEAR(TODAY()), 1, 10) + + Task Created on Account Record {!$Flow.CurrentDateTime} + + + BuilderType + + LightningFlowBuilder + + + + CanvasMode + + AUTO_LAYOUT_CANVAS + + + + OriginBuilderType + + LightningFlowBuilder + + + AutoLaunchedFlow + + NewTask + + 50 + 468 + + ActivityDate + + DueDate + + + + OwnerId + + GroupDetails.Id + + + + Priority + + Normal + + + + Status + + Not Started + + + + Subject + + Update Upcoming Renewals & Reminder Email Template + + + + WhatId + + $Record.Id + + + Task + true + + + GroupDetails + + 50 + 360 + false + + NewTask + + and + + DeveloperName + EqualTo + + AdministrativeTasksQueue + + + + Type + EqualTo + + Queue + + + true + Group + true + + + 56 + 0 + + CheckTodayDate + + or + + Status__c + EqualTo + + Registered Active + + + + Status__c + EqualTo + + Registered Active with Conditions + + + + Status__c + EqualTo + + Registered Active Progressive Enforcement + + + Account + + Daily + 2024-03-01 + 00:00:00.000Z + + Scheduled + + Active + diff --git a/src/main/default/classes/ScheduleSendEmail.cls b/src/main/default/classes/ScheduleSendEmail.cls new file mode 100644 index 000000000..29951ef68 --- /dev/null +++ b/src/main/default/classes/ScheduleSendEmail.cls @@ -0,0 +1,109 @@ +/** +* @Name : ScheduledEmailTrigger +* @Description : Class for Sending Email on Jan-15 +* @Author : Keerthana Srinivasan (Accenture) +* @StoryNo : ALR-562 +**/ + +public with sharing class ScheduleSendEmail implements Schedulable { + + public static Set accountIds = new Set(); + + public void execute(SchedulableContext sc) { + Date currentDate = Date.today(); + if(currentDate.month() == 1 && currentDate.day() == 15){ + try { + Map> accountIdToContactIdsMap = new Map>(); + + List accounts = [ + SELECT Id + FROM Account + WHERE Status__c IN ('Registered Active', 'Registered Active with Conditions', 'Registered Active Progressive Enforcement') + ]; + + for (Account acc : accounts) { + accountIds.add(acc.Id); + accountIdToContactIdsMap.put(acc.Id, new Set()); + } + + Map accountIdToAccountMap = new Map([ + SELECT Id, + (SELECT Id, ContactId, Contact.Name, Contact.Email + FROM AccountContactRelations + WHERE IsActive = true AND Contact.Email != null) + FROM Account + WHERE Id IN :accountIds + ]); + + for (Account acc : accountIdToAccountMap.values()) { + for (AccountContactRelation acr : acc.AccountContactRelations) { + accountIdToContactIdsMap.get(acc.Id).add(acr.ContactId); + } + } + + List contactIdsList = new List(); + for (Set contactIds : accountIdToContactIdsMap.values()) { + contactIdsList.addAll(contactIds); + } + + if (!contactIdsList.isEmpty()) { + sendRenewalDueEmail(contactIdsList); + } + + } catch (Exception e) { + System.debug('Exception occurred in execute method: ' + e.getMessage()); + } + } + } + public static void sendRenewalDueEmail(List contactIds) { + try { + EmailTemplate emailTemplate = getEmailTemplate('Upcoming_Renewal_Email_Template'); + + if (emailTemplate != null && emailTemplate.Id != null) { + OrgWideEmailAddress orgWideAddr = [ + SELECT Id, DisplayName, Address + FROM OrgWideEmailAddress + WHERE DisplayName = 'ALR Support Email' + ]; + + List emails = new List(); + + for (Id contactId : contactIds) { + Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); + email.setTemplateId(emailTemplate.Id); + email.setOrgWideEmailAddressId(orgWideAddr.Id); + email.setTargetObjectId(contactId); + String emailSubject = emailTemplate.Subject; + String emailBody = emailTemplate.HtmlValue; + + email.setSubject(emailSubject); + email.setHtmlBody(emailBody); + emails.add(email); + } + + if (!emails.isEmpty()) { + Messaging.sendEmail(emails); + System.debug('Sent ' + emails.size() + ' email messages successfully.'); + } + } + } catch (Exception e) { + System.debug('Exception occurred in sendRenewalDueEmail method: ' + e.getMessage()); + } + } + + // Method to retrieve email template by Developer Name + public static EmailTemplate getEmailTemplate(String templateDevName) { + EmailTemplate emailTemplateRec = null; + try { + emailTemplateRec = [ + SELECT Id, DeveloperName, Subject, HtmlValue, Body + FROM EmailTemplate + WHERE DeveloperName = :templateDevName + LIMIT 1 + ]; + } catch (Exception ex) { + System.debug('Exception occurred in getEmailTemplate method: ' + ex.getMessage()); + } + return emailTemplateRec; + } +} \ No newline at end of file diff --git a/src/main/default/classes/ScheduleSendEmail.cls-meta.xml b/src/main/default/classes/ScheduleSendEmail.cls-meta.xml new file mode 100644 index 000000000..754ecb155 --- /dev/null +++ b/src/main/default/classes/ScheduleSendEmail.cls-meta.xml @@ -0,0 +1,5 @@ + + + 57.0 + Active + diff --git a/src/main/default/classes/ScheduledEmailTrigger.cls b/src/main/default/classes/ScheduledEmailTrigger.cls new file mode 100644 index 000000000..3073faa31 --- /dev/null +++ b/src/main/default/classes/ScheduledEmailTrigger.cls @@ -0,0 +1,116 @@ +/** +* @Name : ScheduledEmailTrigger +* @Description : Class for Sending Email on March-15 +* @Author : Keerthana Srinivasan (Accenture) +* @StoryNo : ALR-192 +**/ + +public with sharing class ScheduledEmailTrigger implements Schedulable { + public static Set accountIds = new Set(); + + public void execute(SchedulableContext sc) { + Date currentDate = Date.today(); + if(currentDate.month() == 3 && currentDate.day() == 15){ + try { + + Map> accountIdToContactIdsMap = new Map>(); + List fees = [ + SELECT Id,AccountId,ParentRecordId + FROM RegulatoryTrxnFee + WHERE FeeType__c = 'Renewal Unit Fee' + AND Status = 'Due' + ]; + for (RegulatoryTrxnFee fee : fees) { + accountIds.add(fee.AccountId); + accountIdToContactIdsMap.put(fee.AccountId, new Set()); + } + + Map accountIdToAccountMap = new Map([ + SELECT Id,parentId, + (SELECT Id, ContactId, Contact.Name, Contact.Email + FROM AccountContactRelations + WHERE IsActive = true AND Contact.Email != null) + FROM Account + WHERE RecordType.DeveloperName = 'Residence' + AND Id IN :accountIds + ]); + for (Account acc : accountIdToAccountMap.values()) { + for (AccountContactRelation acr : acc.AccountContactRelations) { + accountIdToContactIdsMap.get(acc.Id).add(acr.ContactId); + } + } + + List contactIdsList = new List(); + for (Set contactIds : accountIdToContactIdsMap.values()) { + contactIdsList.addAll(contactIds); + } + if(!contactIdsList.isEmpty()){ + sendRenewalDueEmail(contactIdsList); + } + } + catch (Exception e) { + System.debug('Exception occurred in execute method: ' + e.getMessage()); + } + } + } + public static void sendRenewalDueEmail(List contactIds) { + try { + EmailTemplate emailTemplate = getEmailTemplate('Renewal_Reminder_Email'); + + if (emailTemplate != null && emailTemplate.Id != null) { + OrgWideEmailAddress orgWideAddr = [ + SELECT Id, DisplayName, Address + FROM OrgWideEmailAddress + WHERE DisplayName = 'ALR Support Email' + ]; + + List emails = new List(); + + for (Id contactId : contactIds){ + Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); + email.setTemplateId(emailTemplate.Id); + email.setOrgWideEmailAddressId(orgWideAddr.Id); + email.setTargetObjectId(contactId); + + String emailSubject = emailTemplate.Subject; + String emailBody = emailTemplate.HtmlValue; + + email.setSubject(emailSubject); + email.setHtmlBody(emailBody); + emails.add(email); + } + + if (!emails.isEmpty()) { + List sendResults = Messaging.sendEmail(emails); + + for (Integer i = 0; i < sendResults.size(); i++) { + if (sendResults[i].isSuccess()) { + System.debug('Email sent successfully to ContactId: ' + contactIds[i]); + + } else { + System.debug('Failed to send email to ContactId: ' + contactIds[i] + '. Error: ' + sendResults[i].getErrors()[0].getMessage()); + } + } + } + } + }catch (Exception e) { + System.debug('Exception occurred in sendRenewalDueEmail method: ' + e.getMessage()); + } + } + + public static EmailTemplate getEmailTemplate(String templateDevName) { + EmailTemplate emailTemplateRec = null; + List contactIdsList = new List(); + try { + emailTemplateRec = [ + SELECT Id, DeveloperName, Subject, HtmlValue + FROM EmailTemplate + WHERE DeveloperName = :templateDevName + LIMIT 1 + ]; + } catch (Exception ex) { + System.debug('Exception occurred in getEmailTemplate method: ' + ex.getMessage()); + } + return emailTemplateRec; + } +} \ No newline at end of file diff --git a/src/main/default/classes/ScheduledEmailTrigger.cls-meta.xml b/src/main/default/classes/ScheduledEmailTrigger.cls-meta.xml new file mode 100644 index 000000000..754ecb155 --- /dev/null +++ b/src/main/default/classes/ScheduledEmailTrigger.cls-meta.xml @@ -0,0 +1,5 @@ + + + 57.0 + Active +