diff --git a/cumulusci.yml b/cumulusci.yml index a81aec0c..29db6d99 100644 --- a/cumulusci.yml +++ b/cumulusci.yml @@ -1,15 +1,15 @@ -minimum_cumulusci_version: "3.12.2" +minimum_cumulusci_version: '3.12.2' project: name: 01HousingandHomelessness package: name: ReferralRecommendations namespace: refrec - api_version: "48.0" + api_version: '48.0' # dependencies: # - github: 'https://github.com/SalesforceFoundation/PMM' source_format: sfdx test: - name_match: "Test_%" + name_match: 'Test_%' tasks: robot: @@ -27,20 +27,20 @@ tasks: description: Assigns the package's Admin permset class_path: cumulusci.tasks.sfdx.SFDXOrgTask options: - command: "force:user:permset:assign -n RefRecAdmin" + command: 'force:user:permset:assign -n RefRecAdmin' assign_permset_apex: description: Assigns the package's Admin permset class_path: cumulusci.tasks.apex.anon.AnonymousApexTask options: - path: "scripts/apex/PermSetAssign.apex" + path: 'scripts/apex/PermSetAssign.apex' score_services: description: "Score the org's data and deploy the updated CMDT" - group: "ReferralRecommendations" + group: 'ReferralRecommendations' class_path: cumulusci.tasks.apex.anon.AnonymousApexTask options: - apex: "Scoring.scoreAllServices();" + apex: 'Scoring.scoreAllServices();' run_tests: options: @@ -80,3 +80,17 @@ flows: ignore_failure: True 3: task: score_services + + install_beta: + steps: + 11: + task: assign_permset_apex + ignore_failure: True + 12: + task: load_dataset + options: + mapping: datasets/mapping-ns.yml + 13: + task: score_services + options: + apex: 'RefRec__Scoring.scoreAllServices();' diff --git a/datasets/mapping-ns.yml b/datasets/mapping-ns.yml new file mode 100644 index 00000000..20252613 --- /dev/null +++ b/datasets/mapping-ns.yml @@ -0,0 +1,142 @@ +Insert Account: + sf_object: Account + table: Account + fields: + Id: sf_id + Name: Name +Insert Contact: + sf_object: Contact + table: Contact + fields: + Id: sf_id + FirstName: FirstName + LastName: LastName + MobilePhone: MobilePhone + Email: Email + Birthdate: BirthDate + DoNotCall: DoNotCall + HasOptedOutOfEmail: HasOptedOutOfEmail + HasOptedOutOfFax: HasOptedOutOfFax + lookups: + ReportsToId: + table: Contact + after: Insert Contact + key_field: reports_to_id +Insert RefRec__Client_Profile__c: + sf_object: RefRec__Client_Profile__c + table: Client_Profile__c + fields: + Id: sf_id + RefRec__Date_of_Birth__c: Date_of_Birth__c + RefRec__First_Name__c: First_Name__c + RefRec__Last_Name__c: Last_Name__c + RefRec__Postal_Code__c: Postal_Code__c + RefRec__Location__Latitude__s: Location__Latitude__s + RefRec__Location__Longitude__s: Location__Longitude__s + RefRec__Homeless__c: Homeless__c + RefRec__Runaway__c: Runaway__c + RefRec__Home_Owner__c: Home_Owner__c + RefRec__Home_Renter__c: Home_Renter__c + RefRec__Near_Homeless__c: Near_Homeless__c + RefRec__Employment__c: Employment__c + lookups: + RefRec__Contact__c: + key_field: contact__c + table: Contact +Insert RefRec__Service__c: + sf_object: RefRec__Service__c + table: Service__c + fields: + Id: sf_id + Name: Name + RefRec__Maximum_Age__c: Maximum_Age__c + RefRec__Minimum_Age__c: Minimum_Age__c + RefRec__City__c: City__c + RefRec__Description__c: Description__c + RefRec__Phone__c: Phone__c + RefRec__Street__c: Street__c + RefRec__Type__c: Type__c + RefRec__Website__c: Website__c + RefRec__Zip_Code__c: Zip_Code__c + RefRec__External_ID__c: External_ID__c + RefRec__Preferred__c: Preferred__c + RefRec__Location__Latitude__s: Location__Latitude__s + RefRec__Location__Longitude__s: Location__Longitude__s + RefRec__Homeless__c: Homeless__c + RefRec__Runaway__c: Runaway__c + RefRec__Home_Owner__c: Home_Owner__c + RefRec__Home_Renter__c: Home_Renter__c + RefRec__Near_Homeless__c: Near_Homeless__c + RefRec__Unemployed__c: Unemployed__c + RefRec__Retired__c: Retired__c + RefRec__Employed_part_time__c: Employed_part_time__c + RefRec__Employed_full_time__c: Employed_full_time__c + lookups: + RefRec__Account__c: + table: Account + key_field: account__c +Insert Open_Hours__c: + sf_object: RefRec__Open_Hours__c + table: Open_Hours__c + fields: + Id: sf_id + RefRec__End_Time__c: End_Time__c + RefRec__Start_Time__c: Start_Time__c + RefRec__Day__c: Day__c + lookups: + RefRec__Service__c: + table: Service__c + key_field: service__c +Insert Referral__c: + sf_object: RefRec__Referral__c + table: Referral__c + fields: + Id: sf_id + RefRec__Preferred_Channel__c: Preferred_Channel__c + RefRec__Score__c: Score__c + lookups: + RefRec__Contact__c: + table: Contact + key_field: contact__c + RefRec__Service__c: + table: Service__c + key_field: service__c +Insert Task: + sf_object: Task + table: Task + fields: + Id: sf_id + Subject: Subject + Description: Description + Status: Status + Priority: Priority + lookups: + WhoId: + table: Contact + key_field: who_id + WhatId: + table: Referral__c + key_field: what_id +Insert Service_Exclusion__c: + sf_object: RefRec__Service_Exclusion__c + table: Service_Exclusion__c + fields: + Id: sf_id + lookups: + RefRec__Contact__c: + table: Contact + key_field: contact__c + RefRec__Service__c: + table: Service__c + key_field: service__c +Insert FeedItem: + sf_object: FeedItem + table: FeedItem + fields: + Id: sf_id + Body: Body + Type: Type + lookups: + ParentId: + table: Service__c + key_field: parent_id diff --git a/force-app/main/default/classes/Batch_SendInitialClientReferrals.cls b/force-app/main/default/classes/Batch_SendInitialClientReferrals.cls index c97f18e1..499b1162 100644 --- a/force-app/main/default/classes/Batch_SendInitialClientReferrals.cls +++ b/force-app/main/default/classes/Batch_SendInitialClientReferrals.cls @@ -18,21 +18,18 @@ global class Batch_SendInitialClientReferrals implements Database.Batchable referralTasks) { // get the referral ids from each task List referralIds = new List(); + Map taskToReferralMap = new Map(); 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 diff --git a/force-app/main/default/classes/Batch_SendReferralFollowupReminders.cls b/force-app/main/default/classes/Batch_SendReferralFollowupReminders.cls index 2d9f9b62..722495c3 100644 --- a/force-app/main/default/classes/Batch_SendReferralFollowupReminders.cls +++ b/force-app/main/default/classes/Batch_SendReferralFollowupReminders.cls @@ -19,20 +19,17 @@ global class Batch_SendReferralFollowupReminders implements Database.Batchable referralIds = new List(); + Map taskToReferralMap = new Map(); 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; } } diff --git a/force-app/main/default/classes/ClientMessageHelper.cls b/force-app/main/default/classes/ClientMessageHelper.cls index 8b2c910d..688912a6 100644 --- a/force-app/main/default/classes/ClientMessageHelper.cls +++ b/force-app/main/default/classes/ClientMessageHelper.cls @@ -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 createInitialClientReferralEmails(Map referralMap) { + public static List createInitialClientReferralEmails(Map referralMap, Map taskToReferralMap) { // get the email template for this kind of message EmailTemplate template = getEmailTemplateByName(INITIAL_CLIENT_REFERRAL_TEMPLATE); @@ -33,12 +33,12 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } - public static List createReferralFollowupEmails(Map referralMap) { + public static List createReferralFollowupEmails(Map referralMap, Map taskToReferralMap) { // get the email template for this kind of message EmailTemplate template = getEmailTemplateByName(CLIENT_REFERRAL_FOLLOWUP_TEMPLATE); @@ -47,7 +47,7 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -61,7 +61,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -75,7 +76,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -89,7 +91,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -103,7 +106,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -117,7 +121,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -131,7 +136,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -145,7 +151,8 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List messages = createEmailMessages(referralMap, emailAddress, template, taskToReferralMap); return messages; } @@ -159,12 +166,13 @@ public with sharing class ClientMessageHelper { OrgWideEmailAddress emailAddress = getReferralOrgWideEmailAddress(); // create email messages containing this information - List messages = createEmailMessages(referralMap, emailAddress, template); + Map taskToReferralMap = new Map(); + List 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)) { @@ -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; } @@ -205,31 +213,59 @@ public with sharing class ClientMessageHelper { return template; } - private static List - createEmailMessages(Map referralMap, OrgWideEmailAddress orgWideEmail, EmailTemplate template) { + private static List createEmailMessages(Map referralMap, + OrgWideEmailAddress orgWideEmail, + EmailTemplate template, + Map taskToReferralMap) { List messages = new List(); - List responses = new List(); + List referralResponses = new List(); + 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 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 getContactEmailMap(List emails) { Map contactEmailMap = new Map(); for (Messaging.SingleEmailMessage email : emails) { diff --git a/force-app/main/default/classes/EmailService.cls b/force-app/main/default/classes/EmailService.cls index ab27ed77..61a9e8e5 100644 --- a/force-app/main/default/classes/EmailService.cls +++ b/force-app/main/default/classes/EmailService.cls @@ -1,19 +1,19 @@ public with sharing class EmailService { - public static void sendInitialReferralMessages(List referralIds) { + public static void sendInitialReferralMessages(Map taskToReferralMap) { // get the referral records - Map referralMap = ClientMessageHelper.getReferralRecords(referralIds); + Map referralMap = ClientMessageHelper.getReferralRecords(taskToReferralMap.values()); // create the emails - List emails = ClientMessageHelper.createInitialClientReferralEmails(referralMap); + List emails = ClientMessageHelper.createInitialClientReferralEmails(referralMap, taskToReferralMap); // prep and send the emails prepareAndSendEmails(emails, referralMap); } - public static void sendReferralFollowupMessages(List referralIds) { + public static void sendReferralFollowupMessages(Map taskToReferralMap) { // get the referral records - Map referralMap = ClientMessageHelper.getReferralRecords(referralIds); + Map referralMap = ClientMessageHelper.getReferralRecords(taskToReferralMap.values()); // create the emails - List emails = ClientMessageHelper.createReferralFollowupEmails(referralMap); + List emails = ClientMessageHelper.createReferralFollowupEmails(referralMap, taskToReferralMap); // prep and send the emails prepareAndSendEmails(emails, referralMap); } @@ -107,8 +107,12 @@ public with sharing class EmailService { sendMessageToContact(contactMessages, clientContactMap, referralMap); } - public static void sendMessageToContact (List contactMessages, Map clientContactMap, Map referralMap) { + public static void sendMessageToContact (List contactMessages, + Map clientContactMap, + Map referralMap) { + List contactsToUpdate = new List(); + List tasksToUpdate = new List(); // 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 @@ -120,13 +124,16 @@ public with sharing class EmailService { List referralResponses = [ SELECT Id, - Referral__c + Referral__c, + Task_Id__c FROM Referral_Response__c WHERE Id IN :referralResponseIds ]; Map referralResponseIdToReferralIdMap = new Map(); + Map referralResponseIdToTaskIdMap = new Map(); for (Referral_Response__c refResp : referralResponses) { referralResponseIdToReferralIdMap.put(refResp.Id, refResp.Referral__c); + referralResponseIdToTaskIdMap.put(refResp.Id, refResp.Task_Id__c); } for (ContactMessage contactMsg : contactMessages) { @@ -147,21 +154,26 @@ public with sharing class EmailService { // check the contact if (clientContact != null) { - // if their preferred channel is email, this becomes easier // 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); + // get the task id (if any) from the map + Id taskId = referralResponseIdToTaskIdMap.get(referralResponseId); // determine their preferred channel of contact if ('Email'.equalsIgnoreCase(theReferral.Preferred_Channel__c)) { + // if their preferred channel is email, this becomes easier Messaging.SingleEmailMessage emailToSend = createEmailForContact (messageToSend, clientContact.Email); Boolean emailWasSent = sendEmail(emailToSend); if (!emailWasSent) { throw new EmailServiceException('Failed to successfully send outgoing email to contact'); - } + } else if (taskId != null) { + Task completedTask = createTaskToComplete(taskId); + tasksToUpdate.add(completedTask); + } } else if (clientContact.Phone != null) { // if the preferred channel is SMS, we must use their phone number to create an email to the carrier @@ -209,9 +221,15 @@ public with sharing class EmailService { emailWasSent = sendEmail(emailToSend); if (!emailWasSent) { throw new EmailServiceException('Failed to successfully send outgoing email to carrier on retry'); - } + } else if (taskId != null) { + Task completedTask = createTaskToComplete(taskId); + tasksToUpdate.add(completedTask); + } } - } + } else if (taskId != null) { + Task completedTask = createTaskToComplete(taskId); + tasksToUpdate.add(completedTask); + } // if contacts need to be updated, then add it to the list for update if (contactNeedsUpdate) { @@ -235,6 +253,10 @@ public with sharing class EmailService { if (!contactsToUpdate.isEmpty()) { update contactsToUpdate; } + // update any tasks that have been completed + if (!tasksToUpdate.isEmpty()) { + update tasksToUpdate; + } } @TestVisible @@ -344,6 +366,14 @@ public with sharing class EmailService { } } + private static Task createTaskToComplete(Id taskId) { + Task completedTask = new Task( + Id = taskId, + Status = 'Completed' + ); + return completedTask; + } + public without sharing class ContactMessage { public Contact clientContact; public Messaging.SingleEmailMessage messageToSend; diff --git a/force-app/main/default/classes/ReferralResponseHelper.cls b/force-app/main/default/classes/ReferralResponseHelper.cls index eaf2c2de..d225a410 100644 --- a/force-app/main/default/classes/ReferralResponseHelper.cls +++ b/force-app/main/default/classes/ReferralResponseHelper.cls @@ -10,16 +10,21 @@ public with sharing class ReferralResponseHelper { } } - public static Referral_Response__c createQuestionRecord(String referralId, String question) { + public static Referral_Response__c createQuestionRecord(String referralId, String question, Id taskId) { Referral_Response__c refRes = new Referral_Response__c ( Referral__c = referralId, - Question__c = question + Question__c = question, + Task_Id__c = taskId ); - insert refRes; return refRes; } + public static List saveQuestionRecords(List referralResponses) { + insert referralResponses; + return referralResponses; + } + public static Referral_Response__c updateRecordWithResponse(String referralResponseId, String response) { Referral_Response__c refRes = new Referral_Response__c ( Id = referralresponseId, diff --git a/force-app/main/default/classes/Scoring.cls b/force-app/main/default/classes/Scoring.cls index 83dc601c..a3033493 100644 --- a/force-app/main/default/classes/Scoring.cls +++ b/force-app/main/default/classes/Scoring.cls @@ -1,4 +1,4 @@ -public with sharing class Scoring { +global with sharing class Scoring { // Positive Weights // example metric: my referrals @@ -112,7 +112,7 @@ public with sharing class Scoring { } // Scoring.scoreAllServices(); - public static void scoreAllServices() { + global static void scoreAllServices() { // iterate all metrics for (Scoring_Metric__mdt metricIterator : [ SELECT diff --git a/force-app/main/default/classes/Test_ClientMessageHelper.cls b/force-app/main/default/classes/Test_ClientMessageHelper.cls index 309a58f1..96ad8ff6 100644 --- a/force-app/main/default/classes/Test_ClientMessageHelper.cls +++ b/force-app/main/default/classes/Test_ClientMessageHelper.cls @@ -35,16 +35,27 @@ public class Test_ClientMessageHelper { ); insert ref; + Task refTask = new Task ( + Subject = 'Info to Client', + Status = 'Not Started', + WhoId = cont.Id, + WhatId = ref.Id + ); + insert refTask; + } static testMethod void testCreateInitialClientReferralEmails() { Referral__c ref = getReferral(); + Task refTask = getTaskForReferral(ref.Id); Map referralMap = new Map(); referralMap.put(ref.Id,ref); + Map taskRefMap = new Map(); + taskRefMap.put(refTask.Id,ref.Id); Test.startTest(); - List messages = ClientMessageHelper.createInitialClientReferralEmails(referralMap); + List messages = ClientMessageHelper.createInitialClientReferralEmails(referralMap,taskRefMap); Test.stopTest(); System.assertEquals('Initial Client Referral',messages[0].getTemplateName()); @@ -52,12 +63,15 @@ public class Test_ClientMessageHelper { static testMethod void testCreateReferralFollowupEmails() { Referral__c ref = getReferral(); + Task refTask = getTaskForReferral(ref.Id); Map referralMap = new Map(); referralMap.put(ref.Id,ref); + Map taskRefMap = new Map(); + taskRefMap.put(refTask.Id,ref.Id); Test.startTest(); - List messages = ClientMessageHelper.createReferralFollowupEmails(referralMap); + List messages = ClientMessageHelper.createReferralFollowupEmails(referralMap,taskRefMap); Test.stopTest(); System.assertEquals('Client Referral Followup',messages[0].getTemplateName()); @@ -175,10 +189,11 @@ public class Test_ClientMessageHelper { static testMethod void testSaveReferralQuestion() { Referral__c ref = getReferral(); + Task refTask = getTaskForReferral(ref.Id); Test.startTest(); Referral_Response__c response = - ClientMessageHelper.saveReferralQuestion(ref.Id,ClientMessageHelper.CLIENT_REFERRAL_FOLLOWUP_TEMPLATE); + ClientMessageHelper.createReferralQuestion(ref.Id,ClientMessageHelper.CLIENT_REFERRAL_FOLLOWUP_TEMPLATE,refTask.Id); Test.stopTest(); System.assertEquals(ClientMessageHelper.CLIENT_REFERRAL_FOLLOWUP_QUESTION, response.Question__c); @@ -186,11 +201,14 @@ public class Test_ClientMessageHelper { static testMethod void testGetContactEmailMap() { Referral__c ref = getReferral(); + Task refTask = getTaskForReferral(ref.Id); + Map taskRefMap = new Map(); + taskRefMap.put(refTask.Id,ref.Id); Map referralMap = new Map(); referralMap.put(ref.Id,ref); - List messages = ClientMessageHelper.createInitialClientReferralEmails(referralMap); + List messages = ClientMessageHelper.createInitialClientReferralEmails(referralMap,taskRefMap); Test.startTest(); Map emailMap = ClientMessageHelper.getContactEmailMap(messages); @@ -252,4 +270,13 @@ public class Test_ClientMessageHelper { return ref; } + static Task getTaskForReferral(Id referralId) { + Task refTask = [ + SELECT + Id + FROM Task + WHERE WhatId = :referralId + ][0]; + return refTask; + } } diff --git a/force-app/main/default/classes/Test_EmailService.cls b/force-app/main/default/classes/Test_EmailService.cls index 51e91d57..ecc4ef8f 100644 --- a/force-app/main/default/classes/Test_EmailService.cls +++ b/force-app/main/default/classes/Test_EmailService.cls @@ -35,55 +35,63 @@ public with sharing class Test_EmailService { ); insert ref; + Task refTask = new Task ( + Subject = 'Info to Client', + Status = 'Not Started', + WhoId = cont.Id, + WhatId = ref.Id + ); + insert refTask; + Referral_Response__c refResp = new Referral_Response__c ( Referral__c = ref.Id, Question__c = 'how are you', - Response__c = 'i am fine' + Response__c = 'i am fine', + Task_Id__c = refTask.Id ); insert refResp; } static testMethod void testSendInitialReferralMessages() { - List referrals = [ - SELECT - Id - FROM Referral__c - WHERE Contact__r.LastName = 'Tester' - ]; - List referralIds = new List{referrals[0].Id}; + Referral__c referral = getReferral(); + Task refTask = getTaskForReferral(referral.Id); + Map taskRefMap = new Map(); + taskRefMap.put(refTask.Id,referral.Id); - Test.startTest(); - Boolean exceptionCaught = false; - try { - EmailService.sendInitialReferralMessages(referralIds); - } catch (Exception ex) { + Test.startTest(); + Boolean exceptionCaught = false; + try { + EmailService.sendInitialReferralMessages(taskRefMap); + } catch (Exception ex) { exceptionCaught = true; - } - Test.stopTest(); + } + Test.stopTest(); + + System.assertEquals(false,exceptionCaught); + Task refTaskTest = getTaskForReferral(referral.Id); + System.assertEquals('Completed',refTaskTest.Status); - System.assertEquals(false,exceptionCaught); } static testMethod void testSendReferralFollowupMessages() { - List referrals = [ - SELECT - Id - FROM Referral__c - WHERE Contact__r.LastName = 'Tester' - ]; - List referralIds = new List{referrals[0].Id}; + Referral__c referral = getReferral(); + Task refTask = getTaskForReferral(referral.Id); + Map taskRefMap = new Map(); + taskRefMap.put(refTask.Id,referral.Id); Test.startTest(); Boolean exceptionCaught = false; try { - EmailService.sendReferralFollowupMessages(referralIds); + EmailService.sendReferralFollowupMessages(taskRefMap); } catch (Exception ex) { exceptionCaught = true; } Test.stopTest(); System.assertEquals(false,exceptionCaught); - } + Task refTaskTest = getTaskForReferral(referral.Id); + System.assertEquals('Completed',refTaskTest.Status); + } static testMethod void testSendClientRateExperienceMessage() { List referrals = [ @@ -409,4 +417,24 @@ public with sharing class Test_EmailService { return email; } + static Referral__c getReferral() { + List referrals = [ + SELECT + Id + FROM Referral__c + WHERE Contact__r.LastName = 'Tester' + ]; + return referrals[0]; + } + + static Task getTaskForReferral(Id referralId) { + Task refTask = [ + SELECT + Id, + Status + FROM Task + WHERE WhatId = :referralId + ][0]; + return refTask; + } } diff --git a/force-app/main/default/classes/Test_ReferralResponseHelper.cls b/force-app/main/default/classes/Test_ReferralResponseHelper.cls index 3a7e6cd0..9da325d2 100644 --- a/force-app/main/default/classes/Test_ReferralResponseHelper.cls +++ b/force-app/main/default/classes/Test_ReferralResponseHelper.cls @@ -42,6 +42,14 @@ public class Test_ReferralResponseHelper { Service__c = svc.Id ); insert ref; + + Task refTask = new Task ( + Subject = 'Info to Client', + Status = 'Not Started', + WhoId = cont.Id, + WhatId = ref.Id + ); + insert refTask; } static testMethod void testSaveReferralScore() { @@ -57,18 +65,23 @@ public class Test_ReferralResponseHelper { static testMethod void testCreateQuestionRecord() { Referral__c referral = getReferral(); + Task refTask = getTaskForReferral(referral.Id); Test.startTest(); - ReferralResponseHelper.createQuestionRecord(referral.Id, 'How are you doing?'); + Referral_Response__c refResp = ReferralResponseHelper.createQuestionRecord(referral.Id, 'How are you doing?', refTask.Id); + ReferralResponseHelper.saveQuestionRecords(new List {refResp}); Test.stopTest(); List responses = getReferralResponsesForReferrals(referral.Id); System.assertEquals('How are you doing?',responses[0].Question__c); + System.assertEquals(refTask.Id, responses[0].Task_Id__c); } static testMethod void testUpdateRecordWithResponse() { Referral__c referral = getReferral(); - ReferralResponseHelper.createQuestionRecord(referral.Id, 'How are you doing?'); + Task refTask = getTaskForReferral(referral.Id); + Referral_Response__c refResp = ReferralResponseHelper.createQuestionRecord(referral.Id, 'How are you doing?', refTask.Id); + ReferralResponseHelper.saveQuestionRecords(new List {refResp}); List responses = getReferralResponsesForReferrals(referral.Id); Test.startTest(); @@ -96,10 +109,21 @@ public class Test_ReferralResponseHelper { Id, Referral__c, Question__c, - Response__c + Response__c, + Task_Id__c FROM Referral_Response__c WHERE Referral__c = :referralId ]; return responses; } + + static Task getTaskForReferral(Id referralId) { + Task refTask = [ + SELECT + Id + FROM Task + WHERE WhatId = :referralId + ][0]; + return refTask; + } } diff --git a/force-app/main/default/layouts/Referral_Response__c-Referral Response Layout.layout-meta.xml b/force-app/main/default/layouts/Referral_Response__c-Referral Response Layout.layout-meta.xml index 2fed165e..e942070b 100644 --- a/force-app/main/default/layouts/Referral_Response__c-Referral Response Layout.layout-meta.xml +++ b/force-app/main/default/layouts/Referral_Response__c-Referral Response Layout.layout-meta.xml @@ -23,6 +23,10 @@ Edit Response__c + + Edit + Task_Id__c + diff --git a/force-app/main/default/objects/Referral_Response__c/fields/Task_Id__c.field-meta.xml b/force-app/main/default/objects/Referral_Response__c/fields/Task_Id__c.field-meta.xml new file mode 100644 index 00000000..1758090f --- /dev/null +++ b/force-app/main/default/objects/Referral_Response__c/fields/Task_Id__c.field-meta.xml @@ -0,0 +1,14 @@ + + + Task_Id__c + The 18 character Id of the task that caused this question to be sent + false + The 18 character Id of the task that caused this question to be sent + + 18 + false + false + false + Text + false + diff --git a/scripts/apex/PermSetAssign.apex b/scripts/apex/PermSetAssign.apex index a3f5edd7..835f5f3a 100644 --- a/scripts/apex/PermSetAssign.apex +++ b/scripts/apex/PermSetAssign.apex @@ -1,5 +1,5 @@ PermissionSet PS = [SELECT id FROM PermissionSet WHERE label = 'RefRecAdmin']; -PermissionSetAssignment Existing = [ +list Existing = [ SELECT id FROM PermissionSetAssignment WHERE AssigneeId = :Userinfo.getUserId() AND PermissionSetId = :PS.Id @@ -7,7 +7,7 @@ PermissionSetAssignment Existing = [ system.debug(Existing); -if (Existing == null) { +if (Existing.isEmpty()) { PermissionSetAssignment PSA = new PermissionSetAssignment(); PSA.AssigneeId = Userinfo.getUserId(); PSA.PermissionSetId = PS.Id;