Hubspot is gaining popularity as a lean, cloud-based CRM and marketing automation platform, and new features are being developed continuously. When out-of-the-box solutions are insufficient, custom code is used. In this article, we present a simple custom code solution that allows you to create tasks with a due date based on other date entries (for example, the start date of a deal).
Unfortunately, this is not possible in the standard interface; you can only set the "Days from task creation" setting:
The Problem
Our subsidiary, Lean Hive, offers personnel and freelancer placement. Good support for candidates and clients is crucial in the placement process. We represent each personnel placement in Hubspot as aDeal. Each deal has aStart Date, which is filled with the date of the start of work for the placed person.
To ensure a smooth project start and good follow-up, the business development consultants from Lean Hive additionally make contact at defined intervals:
- 1 month and 3 days before the project start, a call is made with the candidate to clarify any questions and prepare for a good project start.
- On the day of the project start, another call is made, also with the client-side support.
- 8 days after the project start, a call is made to check how well the project start is going.
- 1 month after the project start, the candidate is invited to a lunch where an exchange of experiences and a feedback round takes place.
As soon as a deal moves to theClosed Won stage, the corresponding tasks for the mentioned calls/meetings should be created.
The Solution
What is not possible via the GUI can be easily accomplished with custom code. First, we need to create our Hubspot API key as a secret and select it for the workflow step:
Then we need to define 2 properties. These will be passed from the workflow to the custom code:
- We use the hubspot_owner_id, which is the ID of the deal owner, as the task owner. This way, the task is also assigned to the deal owner.
- We use the start_date as a reference point for our date calculations.
Now we can write the code. First, we write a function called createTask that creates a task in Hubspot:
const hubspot = require('@hubspot/api-client');
const createTask = async (hubspotClient, taskBodyObject) => {
const url = `/engagements/v1/engagements`;
hubspotClient.apiRequest({
path: url,
method: 'POST',
body: taskBodyObject,
json: true
});
}; Next, we write another helper function that can perform the date calculations. It expects a timestamp (in Hubspot, date entries are stored as timestamps) and adds a number of days to this timestamp. To subtract days, i.e., to create a date that is before the provided timestamp, you simply pass a negative number indays.
// Adds "days" days to the timestamp "ts" and returns the new timestamp
const addDaysToTimeStamp = (ts, days) => {
let ret = new Date(ts);
ret.setDate(ret.getDate() + days);
return ret.getTime();
} Then we write one last helper function that provides the body for the task to be created:
const buildDealTaskObject = (taskOwner, timestamp, dealId, subject) => {
return {
engagement: {
active: true,
type: 'TASK',
ownerId: taskOwner,
timestamp: timestamp
},
associations: {
dealIds: [dealId],
},
metadata: {
body: subject,
subject: subject,
status: "NOT_STARTED",
forObjectType: "DEAL"
}
};
} In the main method, we can now create new tasks very simply as follows:
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
const taskOwner = parseInt(event.inputFields['hubspot_owner_id']);
const projectStart = parseInt(event.inputFields['start_date']);
const dealId = event.object.objectId;
const tasksToCreate = [
buildDealTaskObject(taskOwner, projectStart, dealId, "Task at Project Start"),
buildDealTaskObject(taskOwner, addDaysToTimeStamp(projectStart, -5), dealId, "Task 5 Days Before Project Start"),
buildDealTaskObject(taskOwner, addDaysToTimeStamp(projectStart, 5), dealId, "Task 5 Days After Project Start"),
];
tasksToCreate.forEach(task => createTask(hubspotClient, task));
} Conclusion
Custom code solutions should be the last resort. If something can be solved configuratively, it should generally be solved configuratively. In fact, this problem could also be solved configuratively. For this, you would need 2 additional workflows and 1 dynamic list PER TASK. In our opinion, this is too confusing and difficult to maintain. If parameters change, potential intervention is required in 3 configured objects. Therefore, we prefer the custom code solution in this exceptional case.
Here again isthe entire code on GitHub.
Hubspot Automation, Development & Consulting
We offer support on all things Hubspot in the company - from training to workshops to consulting and project support. If you're interested, just reach out via social media or athello@lean-coders.at