Hi all!
I have a use case where I want to automate as much as possible in onboarding our new customers (the onboarding phase is tracked via onboarding tickets). This means at every stage tasks are created, and if all tasks are marked complete the ticket moves to the next stage.
I created a boolean property âHas open workflow tasks?â which gets updated by a workflow every time the last activity date is updated and this gives input when the ticket needs to move to the next stage.
But I found out that the last activity date isnât updated if the completed task is overdue.
Therefore I would like to implement a daily check for tickets with overdue tasks and change the due date to 1 day later. The enrollment filter is working fine, but I canât manage to change the due date. Currently I use the code below but I get errors on the getAll command.
Can anyone help me with this one?
const hubspot = require('@hubspot/api-client');
const request = require("request");
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.secretName
});
// Find all tasks associated with the ticket
hubspotClient.crm.tickets.associationsApi.getAll(event.object.objectId, ["task"])
.then(results => {
// Go through each result returned
results.body.results.forEach(task => {
// Get task details to check hs_task_is_overdue and hs_timestamp properties
hubspotClient.crm.objects.basicApi.getById('task', task.id)
.then(taskDetails => {
const taskProps = taskDetails.body.properties;
if (taskProps.hs_task_is_overdue === 'true') {
// Convert hs_timestamp to an integer, add 1 day (86400 seconds), and convert back to string
const updatedTimestamp = (parseInt(taskProps.hs_timestamp) + 86400).toString();
// Update the task with the new timestamp
const options = {
method: 'PATCH',
url: `https://api.hubapi.com/crm/v3/objects/tasks/${task.id}`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.secretName}`
},
body: {
properties: {
hs_timestamp: updatedTimestamp
}
},
json: true
};
request(options, function (error, response, body) {
if (error) {
throw new Error(error);
}
});
}
})
.catch(error => {
console.error(`Error fetching task details for task ID ${task.id}:`, error);
});
});
})
.catch(error => {
console.error('Error fetching tasks associated with the ticket:', error);
});
};
Thanks in advance!
