💬 RevOps Discussions

SemRC
Participant

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!

0 Upvotes
2 Replies 2
PamCotton
Community Manager
Community Manager

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

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

Você sabia que a Comunidade está disponível em outros idiomas?
Participe de conversas regionais, alterando suas configurações de idioma !


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




0 Upvotes
SemRC
Participant

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

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

0 Upvotes