Skip to content

Commit

Permalink
Merge pull request #132 from Salesforce-org-Impact-Labs/feature/task-…
Browse files Browse the repository at this point in the history
…field-on-responses

add task id field to referral response object
  • Loading branch information
dragongrrl authored Jul 10, 2020
2 parents f14f140 + 7fb0aeb commit b6c24ad
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@ global class Batch_SendInitialClientReferrals implements Database.Batchable<SObj
global void execute(Database.BatchableContext context, List<Task> referralTasks) {
// get the referral ids from each task
List<Id> referralIds = new List<Id>();
Map<Id,Id> taskToReferralMap = new Map<Id,Id>();
for (Task referralTask : referralTasks) {
if (!referralIds.contains(referralTask.WhatId)) {
referralIds.add(referralTask.WhatId);
referralIds.add(referralTask.WhatId); // the whatId holds the id of the referral
}
}
taskToReferralMap.put(referralTask.Id, referralTask.WhatId);
}
// send the emails
if (!referralIds.isEmpty()) {
EmailService.sendInitialReferralMessages(referralIds);
EmailService.sendInitialReferralMessages(taskToReferralMap);
}
// update the tasks to completed
for (Task completedTask : referralTasks) {
completedTask.Status = TASK_STATUS_COMPLETED;
}
update referralTasks;
}
}

global void finish(Database.BatchableContext context) {
// nothing to do here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ global class Batch_SendReferralFollowupReminders implements Database.Batchable<S
if (!reminderTasks.isEmpty()) {
// get the referral ids from each task
List<Id> referralIds = new List<Id>();
Map<Id,Id> taskToReferralMap = new Map<Id,Id>();
for (Task reminderTask : reminderTasks) {
if (!referralIds.contains(reminderTask.WhatId)) {
if (!referralIds.contains(reminderTask.WhatId)) { // the whatId holds the id of the referral
referralIds.add(reminderTask.WhatId);
}
taskToReferralMap.put(reminderTask.Id, reminderTask.WhatId);
}
if (!referralIds.isEmpty()) {
// send the messages
EmailService.sendReferralFollowupMessages(referralIds);
EmailService.sendReferralFollowupMessages(taskToReferralMap);
}
// update the tasks to completed
for (Task completedTask : reminderTasks) {
completedTask.Status = TASK_STATUS_COMPLETED;
}
update reminderTasks;
}
}

Expand Down
96 changes: 66 additions & 30 deletions force-app/main/default/classes/ClientMessageHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public with sharing class ClientMessageHelper {

public static final String CASE_MANAGER_HELP_TEMPLATE = 'Case_Manager_Client_Asked_For_Help';

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

// get the email template for this kind of message
EmailTemplate template = getEmailTemplateByName(INITIAL_CLIENT_REFERRAL_TEMPLATE);
Expand All @@ -33,12 +33,12 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}

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

// get the email template for this kind of message
EmailTemplate template = getEmailTemplateByName(CLIENT_REFERRAL_FOLLOWUP_TEMPLATE);
Expand All @@ -47,7 +47,7 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -61,7 +61,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -75,7 +76,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -89,7 +91,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -103,7 +106,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -117,7 +121,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -131,7 +136,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -145,7 +151,8 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}
Expand All @@ -159,12 +166,13 @@ public with sharing class ClientMessageHelper {
OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress();

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

return messages;
}

public static Referral_Response__c saveReferralQuestion(String referralId, String templateName) {
public static Referral_Response__c createReferralQuestion(String referralId, String templateName, Id taskId) {
String question = '';
// determine the question that was asked, based on the email template
if (INITIAL_CLIENT_REFERRAL_TEMPLATE.equals(templateName)) {
Expand All @@ -189,7 +197,7 @@ public with sharing class ClientMessageHelper {
question = CANT_CONNECT_GUIDANCE_QUESTION_4;
}

Referral_Response__c refRec = ReferralResponseHelper.createQuestionRecord(referralId, question);
Referral_Response__c refRec = ReferralResponseHelper.createQuestionRecord(referralId, question, taskId);
return refRec;
}

Expand All @@ -205,31 +213,59 @@ public with sharing class ClientMessageHelper {
return template;
}

private static List<Messaging.SingleEmailMessage>
createEmailMessages(Map<Id, Referral__c> referralMap, OrgWideEmailAddress orgWideEmail, EmailTemplate template) {
private static List<Messaging.SingleEmailMessage> createEmailMessages(Map<Id, Referral__c> referralMap,
OrgWideEmailAddress orgWideEmail,
EmailTemplate template,
Map<Id,Id> taskToReferralMap) {

List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
List<Referral_Response__c> responses = new List<Referral_Response__c>();
List<Referral_Response__c> referralResponses = new List<Referral_Response__c>();
if (taskToReferralMap.isEmpty()) {
for (Id referralId : referralMap.keySet()) {
// create the referral response record
Id taskId = null;
Referral_Response__c refResp = createReferralQuestion(referralId, template.DeveloperName, taskId);
referralResponses.add(refResp);
}
} else {
for (Id taskId : taskToReferralMap.keySet()) {
// create the referral response record, containing the task id
Id referralId = taskToReferralMap.get(taskId);
Referral_Response__c refResp = createReferralQuestion(referralId, template.DeveloperName, taskId);
referralResponses.add(refResp);
}
}

for (Id referralId : referralMap.keySet()) {
// save the referral response record
Referral_Response__c refResp = saveReferralQuestion(referralId, template.DeveloperName);
// save all the referral question records to the database
referralResponses = ReferralResponseHelper.saveQuestionRecords(referralResponses);

// 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(refResp.Id);
message.setUseSignature(false);
messages.add(message);
// create the email messages
for (Referral_Response__c refResp : referralResponses) {
Messaging.SingleEmailMessage message = createEmail(referralMap, orgWideEmail, template, refResp, refResp.Referral__c);
messages.add(message);
}

return messages;
}

private static Messaging.SingleEmailMessage createEmail(Map<Id, Referral__c> referralMap,
OrgWideEmailAddress orgWideEmail,
EmailTemplate template,
Referral_Response__c refResp,
Id referralId) {

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(refResp.Id);
message.setUseSignature(false);

return message;
}

public static Map<Id,Messaging.SingleEmailMessage> getContactEmailMap(List<Messaging.SingleEmailMessage> emails) {
Map<Id,Messaging.SingleEmailMessage> contactEmailMap = new Map<Id,Messaging.SingleEmailMessage>();
for (Messaging.SingleEmailMessage email : emails) {
Expand Down
Loading

0 comments on commit b6c24ad

Please sign in to comment.