I think I have this solved! Long story short, itâs probably best to save the date provided by the user as a string, and then convert that string into the date as desired using code actions.
Here I have provided ways to do that both within the chat bot, which allows you to have users confirm that the date entered is correct; and in a workflow, which allows you to get the final date value for use elsewhere in your system.
Let me know if any of this needs clarifying!
The Chatbot
This shows a rough outline of the bot itself - key parts are as follows:
- Question that saves the date response as a string to a field on the contact
- Code Snippet that reformats the date and displays it back to the user
- Question that confirms whether the date value returned is correct.
- If/then outcomes on the question that allow the user to go back to the first Question if they entered the wrong date
Code Snippet
The code snippet below (most up to date version is the text, the screenshot just shows roughly what it should look like) does the following:
- Gets the datestring value that was entered by the user
- Splits it by â/â and/or â-â
- Takes the three resulting values, and creates a dd/mm/yyyy date from them.
- Displays that date in UK format
- If thereâs an error (incorrect entry), returns an error message to the user
exports.main = (event, callback) => {
//get the date string that the user input
try {
const customDateString = event.session.properties.CONTACT['customdatestring'].value.trim();
//split the date string on all "/" or "-"
var customDateArray = customDateString.split(/\/|-/);
//if the year was input as only the last two digits, add the '20' to make sure it parses right
if (customDateArray[2].length == 2) {customDateArray[2] = '20'+customDateArray[2]}
//create a new date, using the first value as the day, second value as the month (it's minus one because the index for months starts at 0), and the third value as the year. Indexes in javascript start at 0, so 'value 1' is index 0.
var correctDate = new Date(customDateArray[2], (parseInt(customDateArray[1]) - 1), customDateArray[0].padStart(2, '0'))
//output the date formatted so that your UK readers will see it as they expect
const messageDate = correctDate.toLocaleDateString('en-GB');
//format the message the bot will send
const outputMessage = 'I understood that date as: ' + messageDate;
const responseJson = {
//this is the message the bot sends
botMessage: outputMessage,
//the bot does not expect a response
responseExpectd: false
}
//send the message
callback(responseJson);
}
catch (err) {
const errorMessage = "Incorrect date value entered";
const responseJson = {
//this is the message the bot sends
botMessage: errorMessage,
//the bot does not expect a response
responseExpectd: false
}
//send the message
callback(responseJson);
}
};
Example:
This is what it looks like when working!
Workflow
The workflow shown below takes the datestring, and converts it (correctly) into a hubspot date value.
Steps:
- Trigger step: make sure you have this set to re-enrol on âcustomdatestring is knownâ
- Custom code
- inputs the customdatestring
- outputs âoutputdateâ as a number - this is important
- the outputDate value has to be floored to remove the irrelevant time values
- Format date
- take the outputDate variable from the code, and add 0 days to it, outputing as date and copy into your destination date field, in this case customdatefield
exports.main = async (event, callback) => {
/*****
Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
*****/
const customDateString = event.inputFields['customdatestring'];
console.log(customDateString, typeof(customDateString));
var customDateArray = customDateString.split(/\/|-/);
if (customDateArray[2].length == 2) {customDateArray[2] = '20'+customDateArray[2]}
var correctDate = new Date(customDateArray[2], customDateArray[1], customDateArray[0])
const outputDate = Math.floor(correctDate.getTime());
/*****
Use the callback function to output data that can be used in later actions in your workflow.
*****/
callback({
outputFields: {
outputDate: outputDate
}
});
}