Looking to streamline contract renewals and gain an overview that saves time and minimizes manual work? You're in the right place! Today, we will delve deep into the world of HubSpot Hacks with a focus on Operations Hub. We'll explore an exciting method for automating and correcting dates with custom code. Join us as we guide you through the steps to create a smooth and efficient process for contract renewals that could revolutionize your business! In this week's blog, there will be a little more hands-on HubSpot Hacks, with a focus on the custom code part of Operations Hub!
Content:
Step 1:
We need a reason to create the upcoming automation.
In our example, we will use a custom object called "Vendor Contracts". We are creating this custom object to be able to follow up and get a good overview of our contracts with our suppliers.
Step 2:
Once inside each contract, we will be able to create fields that are relevant for getting a good overview:
We would like to be able to do the following:
Here comes the exciting part! Workflow #1
To do this automatically, we need to use HubSpot's own workflow and custom code features. But before we get to custom code, we need to make sure a few other things work first!
1. We need to know first that it is time to trigger a renewal - we do this in workflow #1.
Kriterierna för detta är:
If all of these are met you trigger a set property action - set time to renew to "yes"
In workflow number 2, we then need to look at whether similar requirements are met.
As step 2 in the same workflow, you create a branch. In this branch, you look at contract renewal months. This is because you will need to adjust the flow accordingly depending on how many months it should be renewed. In this example, you will look at 1 month.
Now it starts to get exciting! First, we will need to format the contract start date and add 1 month to the current contract start date and save it to the same property. We add one month because contract renewal months is set to 1. This is also where it differs per branch, depending on how many months you want to increase at a time, a branch is required for each "scenario".
Next, we will do the same thing on the contract end date, we add one month and save it back to the same property. This is where we will need custom code because HubSpot does not care how many days there are in each month. So it becomes problematic if we have had a contract that has an end date of February 29th. If we add exactly 1 month (according to how HubSpot interprets it) we will get April 29th even though April has 30 days. We solve this with custom code!
So what we do now is we increase the contract end date by one month but we do not save it back to the property.
We will create a custom code action instead.
The settings that need to be made in this are shown in the next image under:
copy the following code into the custom code module.
exports.main = async (event, callback) => {
let inputDate = new Date(parseInt(event.inputFields['inputDate']));
let newDateString = inputDate.getFullYear() + '-' + (inputDate.getMonth() + 2).toString().padStart(2, '0') + '-01 00:00:00 UTC';
var newDate = new Date(Date.parse(newDateString));
newDate.setUTCDate(newDate.getDate() - 1);
console.log('Input date raw: ' + event.inputFields['inputDate']);
console.log('Input date as Date: ' + inputDate);
console.log('New date: ' + newDate);
callback({
outputFields: {
outputDateString: newDate,
outputDate: newDate,
}
Bland det sista vi gör är att skapa en output. Vi behöver hämta ut det nya datumet och spara det i contract end date. Så för att lyckas med detta, klicka på "add output" och skapa en typ "date" och skriv sedan "outputTimeStamp".
In the last step, we perform a copy property action. We want to copy outputTimeStamp, which was the output from custom code. We want to save this in the contract end date.
There you go! You now have a functioning system to follow up on your contracts with the correct date :)
/ Viktor Olsson - CRM Consultant