Skip to content

Commit

Permalink
bulkified email send
Browse files Browse the repository at this point in the history
  • Loading branch information
tcdahlberg committed Jan 10, 2024
1 parent 898553f commit 6952f83
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 51 deletions.
95 changes: 51 additions & 44 deletions force-app/main/default/classes/SummitEventsRegistration.cls
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,38 @@ public class SummitEventsRegistration {
];

List<Task> activityUpdates = new List<Task>();
List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();

//loop through affected event registrations - have status change from old
for (Summit_Events_Registration__c areg : affectedRegs) {
for (Summit_Events_Registration__c registration : affectedRegs) {
//Loop through gather email templates to get template and use registration fields to fill in hooks
for (Summit_Events_Email__c em : matchEmail) {
Boolean statusMatch = false;
Boolean bccRegStatusMatch = false;
List<String> BCCsToGo = new List<String>();
if (em.Event__c.equals(areg.Event__c)) {
if (em.Event__c.equals(registration.Event__c)) {

statusMatch = false;

if (String.isNotBlank(em.Action_Sub_status__c) && String.isNotBlank(areg.Substatus__c)) {
if (em.Action_Sub_status__c.equals(areg.Substatus__c) && em.Action_Status__c.equalsIgnoreCase(areg.Status__c)) {
if (String.isNotBlank(em.Action_Sub_status__c) && String.isNotBlank(registration.Substatus__c)) {
if (em.Action_Sub_status__c.equals(registration.Substatus__c) && em.Action_Status__c.equalsIgnoreCase(registration.Status__c)) {
statusMatch = true;
}
} else if (em.Action_Status__c.equals(areg.Status__c) && String.isBlank(em.Action_Sub_status__c) && String.isBlank(areg.Substatus__c)) {
} else if (em.Action_Status__c.equals(registration.Status__c) && String.isBlank(em.Action_Sub_status__c) && String.isBlank(registration.Substatus__c)) {
statusMatch = true;
if (String.isNotBlank(areg.BCC_Transactional_Email_Statuses__c)) {
if (areg.BCC_Transactional_Email_Statuses__c.contains(em.Action_Status__c)) {
if (String.isNotBlank(registration.BCC_Transactional_Email_Statuses__c)) {
if (registration.BCC_Transactional_Email_Statuses__c.contains(em.Action_Status__c)) {
bccRegStatusMatch = true;
}
}
}

//Build the BCC list. No duplicate emails. Split strings.
BCCsToGo = addToEmailList(em.BCC_Email__c, BCCsToGo);
BCCsToGo = addToEmailList(areg.Registrant_Other_Email__c, BCCsToGo);
BCCsToGo = addToEmailList(areg.Registrant_Parent_Email__c, BCCsToGo);
BCCsToGo = addToEmailList(registration.Registrant_Other_Email__c, BCCsToGo);
BCCsToGo = addToEmailList(registration.Registrant_Parent_Email__c, BCCsToGo);
if (bccRegStatusMatch) {
BCCsToGo = addToEmailList(areg.BCC_Transactional_Emails__c, BCCsToGo);
BCCsToGo = addToEmailList(registration.BCC_Transactional_Emails__c, BCCsToGo);
}

if (statusMatch) {
Expand Down Expand Up @@ -147,7 +148,7 @@ public class SummitEventsRegistration {
//Get the value for hook from registration have to remove any registration object names since we are already in the object
String found = matcher2.group(1).replace(namespace + 'Summit_Events_Registration__c.', '');
try {
found = String.valueOf(areg.get(found));
found = String.valueOf(registration.get(found));
} catch (Exception e) {
found = '';
}
Expand All @@ -156,8 +157,8 @@ public class SummitEventsRegistration {
if (String.isBlank(found)) {
found = '';
}
//replace all hooks with found values from regstration

//replace all hooks with found values from registration
if (emailTags.contains(matcher2.group(1))) {
found = found.replace('_HL_ENCODED_', '<a href="');
found = found.replace('_HL__blank_HL_', '</a>');
Expand All @@ -172,13 +173,28 @@ public class SummitEventsRegistration {
//put email content (after hooks have been replaced with values) into the email template
originalTemplate = originalTemplate.replaceAll('\\[\\[DONT_DELETE_CONTENT_HERE\\]\\]', emailContent);

//send the message
String emailMessage = sendEmail2(areg.Registrant_Email__c, originalTemplate, emailContent, em.Email_Subject__c, em.Org_Email_Id__c, BCCsToGo);
//Create the email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{
registration.Registrant_Email__c
});
mail.setSubject(em.Email_Subject__c);
mail.setBccAddresses(BCCsToGo);
mail.setPlainTextBody(emailContent);
if (String.isBlank(originalTemplate)) {
mail.setHtmlBody(emailContent);
} else {
mail.setHtmlBody(originalTemplate);
}
mail.setOrgWideEmailAddressId(em.Org_Email_Id__c);

//Add the email to the list of emails to send
emailsToSend.add(mail);

//Create an activity task
Task nt = new Task();
nt.WhatId = areg.Id;
nt.WhoId = areg.Contact__c;
nt.WhatId = registration.Id;
nt.WhoId = registration.Contact__c;
nt.Type = 'Email';
nt.ActivityDate = Date.today();
nt.Subject = 'Event status email: ' + em.Action_Status__c + ' - ' + em.Email_Subject__c;
Expand All @@ -187,17 +203,34 @@ public class SummitEventsRegistration {

nt.Description = emailContent.replaceAll('</p>', '\n'); // Replaces </p> (the paragraph closing tag) with a line break, since it is functionally identical to a line break
nt.Description = nt.Description.replaceAll('<(.*?)>', ''); // Replaces angled brackets and everything between with an empty string
nt.Description += '\n\n' + 'Registrant Email: ' + areg.Registrant_Email__c;
nt.Description = emailMessage + '\n\n' + nt.Description;
nt.Description += '\n\n' + 'Registrant Email: ' + registration.Registrant_Email__c;
nt.Description = nt.Description;

//Add the activity task to the list of activity tasks to save
activityUpdates.add(nt);
}

}
}
}

String emailMessage = 'Email successfully sent!';

if (emailsToSend.size() > 0) {
try {
Messaging.sendEmail(emailsToSend);
} catch (Exception ex) {
//If the email fails, we want to add the error message to the activity task description
emailMessage = 'Email failed with this error: ' + ex.getMessage();
System.debug(ex.getMessage());
}
}

if (activityUpdates.size() > 0) {
//Add email status message to the activity task description
for (Task t : activityUpdates) {
t.Description = emailMessage + '\n\n' + t.Description;
}
doCRUD crudToDo = new doCRUD();
crudToDo.savTask(activityUpdates);
}
Expand Down Expand Up @@ -248,32 +281,6 @@ public class SummitEventsRegistration {
return res;
}

public static String sendEmail2(String EmailTo, String HTMLBody, String plainBody, String Subject, Id OrgEmailId, List<String> BccList) {
String emailMessage = 'Email successfully sent!';
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{
EmailTo
});
mail.setSubject(Subject);
mail.setBccAddresses(BccList);
mail.setPlainTextBody(plainBody);
if (String.isBlank(HTMLBody)) {
mail.setHtmlBody(plainBody);
} else {
mail.setHtmlBody(HTMLBody);
}
mail.setOrgWideEmailAddressId(OrgEmailId);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{
mail
});
} catch (Exception ex) {
emailMessage = 'Email failed with this error: ' + ex.getMessage();
System.debug(ex.getMessage());
}
return emailMessage;
}

private without sharing class doCRUD {

public void savTask(List<Task> taskToSave) {
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/pages/SummitEventsCancelReview.page
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Created by Thaddaeus Dahlberg on 5/1/2018.
<apex:define name="sectionNav"/>
<apex:define name="body">
<div class="slds-col slds-size_1-of-1 slds-p-vertical_x-small slds-p-vertical_xx-small">
<apex:messages/>
<apex:messages />
</div>
<apex:form id="SummitEventsRegistrationCancelReview" rendered="{!!eventOver && !eventNotFound && eventRegistration.Status__c != 'Cancelled'}" styleClass="slds-grid slds-wrap">
<div class="slds-col slds-size_1-of-1 slds-p-vertical_x-small slds-p-vertical_xx-small">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<apiVersion>56.0</apiVersion>
<availableInTouch>true</availableInTouch>
<confirmationTokenRequired>false</confirmationTokenRequired>
<label>SummitEventsLetterheadLookup</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>49.0</apiVersion>
<apiVersion>56.0</apiVersion>
<availableInTouch>false</availableInTouch>
<confirmationTokenRequired>false</confirmationTokenRequired>
<label>crowncollege2020</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>49.0</apiVersion>
<apiVersion>56.0</apiVersion>
<status>Active</status>
</ApexTrigger>
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class SummitEventsRegistration_TEST {

List<Summit_Events_Instance__c> seaTestInstances = SummitEventsTestSharedDataFactory.createTestEvent();
Summit_Events_Registration__c seaTestRegistration = SummitEventsTestSharedDataFactory.createEventRegistration(seaTestInstances[1], 'Test', 'Tester', '[email protected]', '55555', '1971-03-22', '2012', null);

Summit_Events__c seaTestEvent = SummitEventsTestSharedDataFactory.getEventRecord(seaTestInstances[1].Event__c);

Test.startTest();
Summit_Events_Email__c email1 = new Summit_Events_Email__c();
email1.Event__c = seaTestInstances[1].Event__c;
email1.Action_Status__c = 'Registered';
Expand All @@ -30,14 +30,15 @@ public class SummitEventsRegistration_TEST {
email2.BCC_Email__c = '[email protected]';
insert email2;

Test.startTest();
seaTestRegistration.Status__c = 'Registered';
update seaTestRegistration;

seaTestRegistration.Substatus__c = 'In Progress';
update seaTestRegistration;

List<Task> emailTasks = [
SELECT Id, Subject
SELECT Id, Subject, Description
FROM Task
WHERE WhatId = :seaTestRegistration.Id
AND Type = 'Email'
Expand All @@ -46,6 +47,7 @@ public class SummitEventsRegistration_TEST {
AND WhoId = :seaTestRegistration.Contact__c
];

System.debug(JSON.serialize(emailTasks));
//Two tasks should have been created when both status emails were sent
System.assertEquals(emailTasks.size(), 2);
Test.stopTest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>49.0</apiVersion>
<apiVersion>56.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 6952f83

Please sign in to comment.