Skip to content

Commit

Permalink
Merge pull request #87 from Salesforce-org-Impact-Labs/feature/track-…
Browse files Browse the repository at this point in the history
…referral-response

have emails use the referral response instead of the referral
  • Loading branch information
dragongrrl authored Jul 1, 2020
2 parents 06a76fa + 231ccb1 commit de0a286
Show file tree
Hide file tree
Showing 37 changed files with 749 additions and 163 deletions.
107 changes: 96 additions & 11 deletions force-app/main/default/classes/ClientMessageHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ public with sharing class ClientMessageHelper {
// email template names
public static final String INITIAL_CLIENT_REFERRAL_TEMPLATE = 'Initial_Client_Referral';
public static final String CLIENT_REFERRAL_FOLLOWUP_TEMPLATE = 'Client_Referral_Followup';
public static final String CLIENT_REFERRAL_FOLLOWUP_TEMPLATE_NAME = 'Client Referral Followup';
public static final String CLIENT_RATE_SERVICE_TEMPLATE = 'Client_Rate_Service_Experience';
public static final String CLIENT_RATE_SERVICE_TEMPLATE_NAME = 'Client Rate Service Experience';
public static final String CLIENT_HELP_RESPONSE_TEMPLATE = 'Client_Help_Response';
public static final String CLIENT_CANT_CONNECT_TEMPLATE = 'Client_Cant_Connect';
public static final String RATE_SERVICE_SAVED_TEMPLATE = 'Rate_Service_Saved_Response';
public static final String CANT_CONNECT_GUIDANCE_TEMPLATE_1 = 'Cant_Connect_Guidance';
public static final String CANT_CONNECT_GUIDANCE_TEMPLATE_2 = 'Cant_Connect_Guidance_2';
public static final String CANT_CONNECT_GUIDANCE_TEMPLATE_3 = 'Cant_Connect_Guidance_3';
public static final String CANT_CONNECT_GUIDANCE_TEMPLATE_4 = 'Cant_Connect_Guidance_4';

public static final String CLIENT_FOLLOWUP_QUESTION = 'Have you met with your referral';
public static final String INITIAL_CLIENT_REFERRAL_QUESTION = 'Heres your referral information';
public static final String CLIENT_REFERRAL_FOLLOWUP_QUESTION = 'Have you met with your referral';
public static final String CLIENT_RATE_SERVICE_QUESTION = 'Rate your experience on a scale of 1-5';
public static final String CLIENT_HELP_RESPONSE_QUESTION = 'Thank you for letting us know you need help';
public static final String CLIENT_CANT_CONNECT_QUESTION = 'What’s getting in the way of connecting with the service';
public static final String RATE_SERVICE_SAVED_QUESTION = 'Your feedback helps us know which services to recommend to others.';
public static final String CANT_CONNECT_GUIDANCE_QUESTION_1 = 'We know life gets busy.';
public static final String CANT_CONNECT_GUIDANCE_QUESTION_2 = 'We are sorry to hear they didn’t respond.';
public static final String CANT_CONNECT_GUIDANCE_QUESTION_3 = 'We are sorry to hear the information was wrong.';
public static final String CANT_CONNECT_GUIDANCE_QUESTION_4 = 'Can you tell me what happened';

public static List<Messaging.SingleEmailMessage> createInitialClientReferralEmails(Map<Id, Referral__c> referralMap) {

Expand Down Expand Up @@ -96,15 +106,85 @@ public with sharing class ClientMessageHelper {
return messages;
}

public static Referral_Response__c saveReferralQuestion(Messaging.SingleEmailMessage email) {
// the referral id will be in the what id of the email message
String referralId = email.getWhatId();
public static List<Messaging.SingleEmailMessage> createCantConnectGuidance1Emails(Map<Id, Referral__c> referralMap) {

// get the email template for this kind of message
EmailTemplate template = getEmailTemplateByName(CANT_CONNECT_GUIDANCE_TEMPLATE_1);

// get the org wide email for the reply-to
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

// create email messages containing this information
List<Messaging.SingleEmailMessage> messages = createEmailMessages(referralMap, emailAddress, template);

return messages;
}

public static List<Messaging.SingleEmailMessage> createCantConnectGuidance2Emails(Map<Id, Referral__c> referralMap) {

// get the email template for this kind of message
EmailTemplate template = getEmailTemplateByName(CANT_CONNECT_GUIDANCE_TEMPLATE_2);

// get the org wide email for the reply-to
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

// create email messages containing this information
List<Messaging.SingleEmailMessage> messages = createEmailMessages(referralMap, emailAddress, template);

return messages;
}

public static List<Messaging.SingleEmailMessage> createCantConnectGuidance3Emails(Map<Id, Referral__c> referralMap) {

// get the email template for this kind of message
EmailTemplate template = getEmailTemplateByName(CANT_CONNECT_GUIDANCE_TEMPLATE_3);

// get the org wide email for the reply-to
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

// create email messages containing this information
List<Messaging.SingleEmailMessage> messages = createEmailMessages(referralMap, emailAddress, template);

return messages;
}

public static List<Messaging.SingleEmailMessage> createCantConnectGuidance4Emails(Map<Id, Referral__c> referralMap) {

// get the email template for this kind of message
EmailTemplate template = getEmailTemplateByName(CANT_CONNECT_GUIDANCE_TEMPLATE_4);

// get the org wide email for the reply-to
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

// create email messages containing this information
List<Messaging.SingleEmailMessage> messages = createEmailMessages(referralMap, emailAddress, template);

return messages;
}

public static Referral_Response__c saveReferralQuestion(String referralId, String templateName) {
String question = '';
// determine the question that was asked, based on the email template
if (CLIENT_REFERRAL_FOLLOWUP_TEMPLATE_NAME.equals(email.getTemplateName())) {
question = CLIENT_FOLLOWUP_QUESTION;
} else if (CLIENT_RATE_SERVICE_TEMPLATE_NAME.equals(email.getTemplateName())) {
question = CLIENT_RATE_SERVICE_QUESTION;
if (INITIAL_CLIENT_REFERRAL_TEMPLATE.equals(templateName)) {
question = INITIAL_CLIENT_REFERRAL_QUESTION;
} else if (CLIENT_REFERRAL_FOLLOWUP_TEMPLATE.equals(templateName)) {
question = CLIENT_REFERRAL_FOLLOWUP_QUESTION;
} else if (CLIENT_RATE_SERVICE_TEMPLATE.equals(templateName)) {
question = CLIENT_RATE_SERVICE_QUESTION;
} else if (CLIENT_HELP_RESPONSE_TEMPLATE.equals(templateName)) {
question = CLIENT_HELP_RESPONSE_QUESTION;
} else if (CLIENT_CANT_CONNECT_TEMPLATE.equals(templateName)) {
question = CLIENT_CANT_CONNECT_QUESTION;
} else if (RATE_SERVICE_SAVED_TEMPLATE.equals(templateName)) {
question = RATE_SERVICE_SAVED_QUESTION;
} else if (CANT_CONNECT_GUIDANCE_TEMPLATE_1.equals(templateName)) {
question = CANT_CONNECT_GUIDANCE_QUESTION_1;
} else if (CANT_CONNECT_GUIDANCE_TEMPLATE_2.equals(templateName)) {
question = CANT_CONNECT_GUIDANCE_QUESTION_2;
} else if (CANT_CONNECT_GUIDANCE_TEMPLATE_3.equals(templateName)) {
question = CANT_CONNECT_GUIDANCE_QUESTION_3;
} else if (CANT_CONNECT_GUIDANCE_TEMPLATE_4.equals(templateName)) {
question = CANT_CONNECT_GUIDANCE_QUESTION_4;
}

Referral_Response__c refRec = ReferralResponseHelper.createQuestionRecord(referralId, question);
Expand All @@ -127,15 +207,20 @@ public with sharing class ClientMessageHelper {
createEmailMessages(Map<Id, Referral__c> referralMap, OrgWideEmailAddress orgWideEmail, EmailTemplate template) {

List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
List<Referral_Response__c> responses = new List<Referral_Response__c>();

for (Id referralId : referralMap.keySet()) {
// save the referral response record
Referral_Response__c refResp = saveReferralQuestion(referralId, template.DeveloperName);

// create the email message
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
if (orgWideEmail != null) {
message.setOrgWideEmailAddressId(orgWideEmail.Id);
}
message.setTemplateId(template.Id);
message.setTargetObjectId(referralMap.get(referralId).Contact__c);
message.setWhatId(referralId);
message.setWhatId(refResp.Id);
message.setUseSignature(false);
messages.add(message);
}
Expand Down
65 changes: 59 additions & 6 deletions force-app/main/default/classes/EmailService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,42 @@ public with sharing class EmailService {
prepareAndSendEmails(emails, referralMap);
}

public static void sendCantConnectGuidance1Message(Id referralId) {
// get the referral records
Map<Id, Referral__c> referralMap = ClientMessageHelper.getReferralRecords(new List<Id>{referralId});
// create the emails
List<Messaging.SingleEmailMessage> emails = ClientMessageHelper.createCantConnectGuidance1Emails(referralMap);
// prep and send the emails
prepareAndSendEmails(emails, referralMap);
}

public static void sendCantConnectGuidance2Message(Id referralId) {
// get the referral records
Map<Id, Referral__c> referralMap = ClientMessageHelper.getReferralRecords(new List<Id>{referralId});
// create the emails
List<Messaging.SingleEmailMessage> emails = ClientMessageHelper.createCantConnectGuidance2Emails(referralMap);
// prep and send the emails
prepareAndSendEmails(emails, referralMap);
}

public static void sendCantConnectGuidance3Message(Id referralId) {
// get the referral records
Map<Id, Referral__c> referralMap = ClientMessageHelper.getReferralRecords(new List<Id>{referralId});
// create the emails
List<Messaging.SingleEmailMessage> emails = ClientMessageHelper.createCantConnectGuidance3Emails(referralMap);
// prep and send the emails
prepareAndSendEmails(emails, referralMap);
}

public static void sendCantConnectGuidance4Message(Id referralId) {
// get the referral records
Map<Id, Referral__c> referralMap = ClientMessageHelper.getReferralRecords(new List<Id>{referralId});
// create the emails
List<Messaging.SingleEmailMessage> emails = ClientMessageHelper.createCantConnectGuidance4Emails(referralMap);
// prep and send the emails
prepareAndSendEmails(emails, referralMap);
}

public static void prepareAndSendEmails(List<Messaging.SingleEmailMessage> emails, Map<Id, Referral__c> referralMap) {
Map<Id,Messaging.SingleEmailMessage> contactIdEmailMap = ClientMessageHelper.getContactEmailMap(emails);

Expand All @@ -73,6 +109,25 @@ public with sharing class EmailService {
public static void sendMessageToContact (List<ContactMessage> contactMessages, Map<Id,Contact> clientContactMap, Map<Id, Referral__c> referralMap) {
List<Contact> contactsToUpdate = new List<Contact>();

// we need to get the referral id from the referral response record
// the id to this record will be in the WhatId field on the email
Set<Id> referralResponseIds = new Set<Id>();
for (ContactMessage contMsg : contactMessages) {
Messaging.SingleEmailMessage email = contMsg.messageToSend;
referralResponseIds.add(email.getWhatId());
}
List<Referral_Response__c> referralResponses = [
SELECT
Id,
Referral__c
FROM Referral_Response__c
WHERE Id IN :referralResponseIds
];
Map<Id, Id> referralResponseIdToReferralIdMap = new Map<Id, Id>();
for (Referral_Response__c refResp : referralResponses) {
referralResponseIdToReferralIdMap.put(refResp.Id, refResp.Referral__c);
}

for (ContactMessage contactMsg : contactMessages) {
// handle each contact/message, one record at a time
Contact clientContact = contactMsg.clientContact;
Expand All @@ -92,8 +147,9 @@ public with sharing class EmailService {
if (clientContact != null) {

// if their preferred channel is email, this becomes easier
// get the referral id from the email message, it will be the WhatId
Id referralId = messageToSend.getWhatId();
// get the referral id from the email message, via the referral response
Id referralResponseId = messageToSend.getWhatId();
Id referralId = referralResponseIdToReferralIdMap.get(referralResponseId);
// get the referral from the map
Referral__c theReferral = referralMap.get(referralId);

Expand Down Expand Up @@ -185,10 +241,7 @@ public with sharing class EmailService {
Messaging.SendEmailResult[] results = Messaging.sendEmail( new List<Messaging.SingleEmailMessage>{ emailToSend } );

for (Messaging.SendEmailResult result : results) {
if (result.isSuccess()) {
// email was sent, save the question to a referral response record...
ClientMessageHelper.saveReferralQuestion(emailToSend);
} else {
if (!result.isSuccess()) {
Messaging.SendEmailError[] errors = result.getErrors();
for (Messaging.SendEmailError error : errors) {
// TODO is this okay?
Expand Down
Loading

0 comments on commit de0a286

Please sign in to comment.