From 00073516021acf9ecbb463a8453bcdfa779f05c7 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 22 Aug 2024 12:34:13 -0600 Subject: [PATCH] Website: send start flow responses to CRM (#21485) Changes: - Updated save-questionnaire-progress to send a formatted string of a user's start questionnaire responses to the update-or-create-contact-and-account helper - Updated the update-or-create-contact-and-account helper to set getStartedResponses on contact records. --- .../save-questionnaire-progress.js | 21 +++++++++++++------ .../update-or-create-contact-and-account.js | 9 +++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/website/api/controllers/save-questionnaire-progress.js b/website/api/controllers/save-questionnaire-progress.js index 0b1e09fad5..d2106c9c20 100644 --- a/website/api/controllers/save-questionnaire-progress.js +++ b/website/api/controllers/save-questionnaire-progress.js @@ -187,7 +187,20 @@ module.exports = { psychologicalStage = '2 - Aware'; }//fi }//fi - + // Set the user's answer to the current step. + questionnaireProgress[currentStep] = formData; + // Clone the questionnaireProgress to prevent any mutations from sending it through the updateOne Waterline method. + let getStartedProgress = _.clone(questionnaireProgress); + let questionnaireProgressAsAFormattedString = undefined;// Default to undefined. + // Using a try catch block to handle errors from JSON.stringify. + try { + questionnaireProgressAsAFormattedString = JSON.stringify(getStartedProgress) + .replace(/[\{|\}|"]/g, '')// Remove the curly braces and quotation marks wrapping JSON objects + .replace(/,/g, '\n')// Replace commas with newlines. + .replace(/:\w+:/g, ':\t');// Replace the key from the formData with a color and tab, (e.g., what-are-you-using-fleet-for:primaryBuyingSituation:eo-security, » what-are-you-using-fleet-for: eo-security) + } catch(err){ + sails.log.warn(`When converting a user's (email: ${this.req.me.emailAddress}) getStartedQuestionnaireAnswers to a formatted string to send to the CRM, and error occurred`, err); + } // Only update CRM records if the user's psychological stage changes. if(psychologicalStage !== userRecord.psychologicalStage) { // Update the psychologicalStageLastChangedAt timestamp if the user's psychological stage @@ -199,6 +212,7 @@ module.exports = { primaryBuyingSituation: primaryBuyingSituation === 'eo-security' ? 'Endpoint operations - Security' : primaryBuyingSituation === 'eo-it' ? 'Endpoint operations - IT' : primaryBuyingSituation === 'mdm' ? 'Device management (MDM)' : primaryBuyingSituation === 'vm' ? 'Vulnerability management' : undefined, organization: this.req.me.organization, psychologicalStage, + getStartedResponses: questionnaireProgressAsAFormattedString, contactSource: 'Website - Sign up', }).exec((err)=>{ if(err){ @@ -207,11 +221,6 @@ module.exports = { return; }); }//fi - // TODO: send all other answers to Salesforce (when there are fields for them) - // Set the user's answer to the current step. - questionnaireProgress[currentStep] = formData; - // Clone the questionnaireProgress to prevent any mutations from sending it through the updateOne Waterline method. - let getStartedProgress = _.clone(questionnaireProgress); // Update the user's database model. await User.updateOne({id: userRecord.id}) .set({ diff --git a/website/api/helpers/salesforce/update-or-create-contact-and-account.js b/website/api/helpers/salesforce/update-or-create-contact-and-account.js index 8c483c83bb..208e48f406 100644 --- a/website/api/helpers/salesforce/update-or-create-contact-and-account.js +++ b/website/api/helpers/salesforce/update-or-create-contact-and-account.js @@ -37,6 +37,10 @@ module.exports = { 'Website - Sign up', ], }, + getStartedResponses: { + type: 'string', + } + }, @@ -52,7 +56,7 @@ module.exports = { }, - fn: async function ({emailAddress, linkedinUrl, firstName, lastName, organization, primaryBuyingSituation, psychologicalStage, contactSource, description}) { + fn: async function ({emailAddress, linkedinUrl, firstName, lastName, organization, primaryBuyingSituation, psychologicalStage, contactSource, description, getStartedResponses}) { // Return undefined if we're not running in a production environment. if(sails.config.environment !== 'production') { sails.log.verbose('Skipping Salesforce integration...'); @@ -96,6 +100,9 @@ module.exports = { if(psychologicalStage) { valuesToSet.Stage__c = psychologicalStage;// eslint-disable-line camelcase } + if(getStartedResponses) { + valuesToSet.Website_questionnaire_answers__c = getStartedResponses;// eslint-disable-line camelcase + } if(description) { valuesToSet.Description = description; }