Custom workflow coding to find and update all overdue tasks associated to a ticket

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!

Hey @SemRC, thank you for posting in our Community!

Please, ensure you have the correct permissions and scopes for your HubSpot API key. To our top experts @Anton and @DanielSanchez do you have any recommendations for @SemRC matter?

Thank you,

Pam

Dear Pam,

Thanks for your quick response!

Does this mean it’s only possible via an external app / API? I thought the custum coding would also be possible to let Hubspot perform certain actions (so with no API key needed).

Kind regards,

Sem

Hey SemRC!

We’re also looking at a workflow to close open tasks for tickets that are closed. We’ve also run into errors with the getAll command - did you find a solution for this? Was it an issue with the API?

Cheers :slightly_smiling_face:

Hi @SemRC,

Good news, HubSpot has introduced task workflows! This does not require coding anymore.

Workflows
Task workflows
November 7, 2024

What is it?

Users can now create task-centered workflows.

Why does it matter?

Enhance task automation with greater flexibility and efficiency. Trigger automation directly from changes in tasks, like creation, status updates, or overdue notifications. Establish standard, automated processes for task management. For example, alert a sales manager when specific tasks complete or become overdue, or automatically create tasks in other project management tools when you start tasks in HubSpot.

How does it work?

To create a task workflow:

From the workflows tool homepage, click “Create workflow” and select “From scratch”
Choose the task object on the object select page
Choose the trigger based on task events or properties
Add actions as usual

Who gets it?

Marketing Hub Pro, Marketing Hub Enterprise, Operations Hub Pro, Operations Hub Enterprise, Sales Hub Pro, Sales Hub Enterprise, Service Hub Pro, Service Hub Enterprise

You can find this beta here:

You can now enroll tasks based on criteria of your liking and use the ‘Edit record’ workflow action to set a task status to completed:

If you want to make it easier for others to find this update, you can help them by accepting my reply as a solution. I’d appreciate it, too.

Have a great day!

In the end, I was able to reach the same when ticket lists became available (create a list with open task filter, and let the list membership change trigger a workflow to change the “open tasks” property). But indeed the solution mentioned by Karsten would also work.