[TUTORIAL] Custom Code to Write Day of Week and Suggest a Meeting Window
I'm seeing a lot of newer email platforms popping up with the abililty to use if/else statements as email personalizations. Here's an example to give you the gist.
After a lot of trial and error, I've worked out a solution to make HubSpot do the same (or similar) with custom code. While this isn't groundbreaking for the devs out there, I thought I'd post my solution for the technical marketers and ops folks out there.
NOTE: this is a hybrid solution. I know you could skip a couple of workflow actions I've detailed below and copy the properties from the one custom code action, but adding the steps in is good enough for me, for now. Other folks very welcome to suggest enhancements.
Okay, so I needed a way with HubSpot to be able to say seomthing like "if Today = Friday, then add a meeting window of "early next week."
And while I'm at it, I wanted to go on to use the day of the week to open the email with something like "Happy Friday!"
AND... I also needed the timezone to be correct.
This solution would be SUPER helpful to use for folks who are emailing on - or - to totally different continents.
You could adjust the timezone to whatever you want (menaing yours or the recipient's).
The locale could also be adjusted to yield the day of week in the correct language - that's the "en-us" part. If you wanted a day of the week in German, you'd use "de-DE," for example.
Moving on... Particularly if you want to use this in Sequences, you'll need two custom proerties for this solution, one for Day of Week and one for Meeting Window. I created mine as Single-Line Text properties.
I then created a workflow with a custom code action and added the following JS:
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 day = new Date().toLocaleDateString('en-us', { timeZone: "America/Denver", weekday:"long"})
let meeting_window = "";
if (day == 'Sunday') {
meeting_window = "tomorrow or Tuesday";
}
else if (day == 'Monday') {
meeting_window = "tomorrow or Wednesday";
}
else if (day == 'Tuesday') {
meeting_window = "tomorrow or Thursday";
}
else if (day == 'Wednesday') {
meeting_window = "tomorrow or Friday";
}
else if (day == 'Thursday') {
meeting_window = "early next week";
}
else if (day == 'Friday') {
meeting_window = "early next week";
}
else if (day == 'Saturday') {
meeting_window = "Monday or Tuesday";
}
else {
// do nothing
}
/*****
Use the callback function to output data that can be used in later actions in your workflow.
*****/
callback({
outputFields: {
day: day,
meeting_window: meeting_window
}
});
}
Be sure to add Data outputs for "day" and "meeting_window" if you'd like to use this code block without altering it. Since I created my properties as Single-Line Text, the Data type of my Data output is set to "String."
For context, the day part is the part that says "if today = Tuesday" and goes on to use meeting_window to build the next part which says "then meeting_window = "tomorrow or Thursday."
Once you've tested your code successfully, you can then go on to add another workflow action to Copy property value. At the time of this writing, when you select a "Property or value to copy from," you have to scrolll alllll the way to the bottom (or collapse the first section) to get to the "Custom code option."
First, you'll copy the property value for "day" into the Day of Week property you created, and then create a second action for "meeting_window" and do the same.
And then you can add your email(s) in or enroll users in a sequence. Of course, your emails will need the properties/tokens added in, but that's a side note 🙂
A couple of other notes:
If you have a series of emails in one workflow that rely on this personalization, you'll need to do these steps before each send so that your day of week is always correct. Else, you'll be suggesting to meet "early next week" for emails goign out on a Monday, for example. This would also be a +1 for doing it all within one code block and skipping the copy property steps. I'll get there, I'm sure, but I'm learning for now. Again, other folks are welcome to uplevel this part!
If anyone has a list of timezone and/or locale values to share, I'm sure that'd be helpful for non-US folks, etc. I didn't find anything that fully references those values, and I'm not sure about the standard JS draws from.
That's all for now. I hope this is helpful for someone!