diff --git a/src/main/default/classes/ScheduleSendEmail.cls b/src/main/default/classes/ScheduleSendEmail.cls index c77b34bf..a3ea5c9d 100644 --- a/src/main/default/classes/ScheduleSendEmail.cls +++ b/src/main/default/classes/ScheduleSendEmail.cls @@ -7,67 +7,10 @@ public with sharing class ScheduleSendEmail implements Schedulable { - public static Set accountIds = new Set(); + List contacts =new List(); + List emails = new List(); - public void execute(SchedulableContext sc) { - try { - Map> accountIdToContactIdsMap = new Map>(); - - List accounts = [ - SELECT Id ,ParentId - FROM Account - WHERE Status__c IN ('Registered Active', 'Registered Active with Conditions', 'Registered Active Progressive Enforcement') - ]; - - for (Account acc : accounts) { - accountIds.add(acc.Id); - accountIds.add(acc.ParentId); - accountIdToContactIdsMap.put(acc.Id, new Set()); - accountIdToContactIdsMap.put(acc.ParentId, 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 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); - } - - 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) { - List accListforKeepingIds = new List(); + public void execute(SchedulableContext sc) { try { EmailTemplate emailTemplate = getEmailTemplate('Upcoming_Renewal_Email_Template'); @@ -76,66 +19,61 @@ public with sharing class ScheduleSendEmail implements Schedulable { FROM OrgWideEmailAddress WHERE DisplayName = 'ALR Support Email' ]; - List 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 emails = new List(); - Map> processedEmails = new Map>(); - Integer currentyear = Date.today().year(); - Integer previousyear = Date.today().year() - 1; - - 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; - emailBody = emailBody.Replace('{%%Account.Name%%}', acrList.Account.Name); - emailBody = emailBody.Replace('{%%CurrentYear%%}', String.valueOf(currentyear)); - emailBody = emailBody.Replace('{%%PreviousYear%%}', String.valueOf(previousyear)); - emailSubject = emailSubject.Replace('{%%CurrentYear%%}', String.valueOf(currentyear)); - emailSubject = emailSubject.Replace('{%%PreviousYear%%}', String.valueOf(previousyear)); - email.setSubject(emailSubject); - email.setHtmlBody(emailBody); - emails.add(email); - processedEmails.put(key, new List{acrList.contact.Email}); - } - } - } - - + for(Account accounts : [SELECT Id ,Name,ParentId FROM Account + WHERE Status__c IN ('Registered Active', 'Registered Active with Conditions', 'Registered Active Progressive Enforcement') + ]){ + contacts = getContacts(accounts.Id,accounts.ParentId); + Map> processedEmails = new Map>(); + Integer currentyear = Date.today().year(); + Integer previousyear = Date.today().year() - 1; + + for (AccountContactRelation acrList : contacts) { + if (acrList.contact.Email != null) { + String key = acrList.AccountId + '_' + acrList.contact.Email.toLowerCase(); + 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; + emailBody = emailBody.Replace('{%%Account.Name%%}', accounts.Name); + emailBody = emailBody.Replace('{%%CurrentYear%%}', String.valueOf(currentyear)); + emailBody = emailBody.Replace('{%%PreviousYear%%}', String.valueOf(previousyear)); + emailSubject = emailSubject.Replace('{%%CurrentYear%%}', String.valueOf(currentyear)); + emailSubject = emailSubject.Replace('{%%PreviousYear%%}', String.valueOf(previousyear)); + email.setSubject(emailSubject); + email.setHtmlBody(emailBody); + emails.add(email); + } + } + } if (!emails.isEmpty()) { sendEmailBatch(emails); } } catch (Exception e) { - System.debug('Exception occurred in sendRenewalDueEmail method: ' + e.getMessage()); + System.debug('Exception occurred in execute method: ' + e.getMessage()); } } + public Static List getContacts(Id accountId, Id parentId){ + return [Select Id,ContactId,AccountId,IsActive,Contact.Email from AccountContactRelation where (AccountId = :accountId OR AccountId =: parentId) AND IsActive =true AND Contact.Email != NULL]; + } + + public static void sendEmailBatch(List emailBatch) { List singleEmailList = new List(); try{ for (Messaging.SingleEmailMessage email : emailBatch) { singleEmailList.add(email); - } - List results = Messaging.sendEmail(singleEmailList, false); - + } + List results = Messaging.sendEmail(singleEmailList, false); }catch (Exception e) { - System.debug('Exception occurred while sending email to ' + e.getMessage()); - System.debug('Exception occurred while sending email to ' + e.getLineNumber()); - } + System.debug('Exception occurred while sending email to ' + e.getMessage()); } - - + } + public static EmailTemplate getEmailTemplate(String templateDevName) { EmailTemplate emailTemplateRec = null; diff --git a/src/main/default/classes/ScheduleSendEmailTest.cls b/src/main/default/classes/ScheduleSendEmailTest.cls index 45faea32..2a9740e2 100644 --- a/src/main/default/classes/ScheduleSendEmailTest.cls +++ b/src/main/default/classes/ScheduleSendEmailTest.cls @@ -16,15 +16,17 @@ private class ScheduleSendEmailTest { Account acc = TestDataFactory.createResidence('Residence', 'residence1', regAuth.Id, true); accList.add(acc); - Account accRec = [SELECT Id ,ParentId FROM Account where Id =: acc.Id]; + Account accRec = [SELECT Id , Name , ParentId FROM Account where Id =: acc.Id]; Contact con = new Contact( FirstName = 'Test', LastName = 'Contact', - Email = 'test.contact@example.com' + Email = 'test.contact@example.com', + AccountId=accRec.Id ); insert con; - + + AccountContactRelation acr = new AccountContactRelation( AccountId = acc.Id, ContactId = con.Id, @@ -39,30 +41,5 @@ private class ScheduleSendEmailTest { List emailTem = [SELECT Id FROM EmailTemplate]; System.assertEquals(True, emailTem.size()>0, 'No Email is sent'); } - - @isTest - static void testSendRenewalDueEmail() { - - - List accList = new List(); - - // Create an instance of DMLOptions - Database.DMLOptions dmlOptions = new Database.DMLOptions(); - dmlOptions.DuplicateRuleHeader.allowSave = true; - - RegulatoryAuthorizationType regAuth = TestDataFactory.createRegAuth('Mental Health', 'License', 'MH', 1, 2, 3, 4, true); - - Account Residence = TestDataFactory.createResidence('Residence', 'hgvfjmhhgsu', regAuth.Id, true); - - Contact contactRec = TestDataFactory.createContactRecord(Residence.Id, 'test@xyz.com', 'uysgyuskgfh', 'hsguh', true); - - //AccountContactRelation acConRelRec = TestDataFactory.createAccContRelRecord(Residence.Id, contactRec.Id , true, true, true); - - List contactIds = new List{ contactRec.Id }; - Test.startTest(); - ScheduleSendEmail.sendRenewalDueEmail(contactIds); - Test.stopTest(); - List emailTem = [SELECT Id FROM EmailTemplate]; - System.assertEquals(True, emailTem.size()>0, 'No Email is sent'); - } + } \ No newline at end of file