How to Automatically Change Owners of Open Tasks

Deals change owners often, but HubSpot does not change your task owners automatically.

At first, it's not an issue.

Then, leads get missed, productivity dips, and your team is frustrated.

Here's a quick script that can solve your problem in the blink of an eye.

The Messy Task Problem

Your team is confused on who needs to be follow up on critical tasks. They wish tasks could change owners when deals change owners.

The Task Ownership Solution

The solution is simple. A custom code workflow using the HubSpot custom code feature in Operations Hub that changes task owners when deal owners change. You can repurpose this for tickets, companies, contacts, and any object in HubSpot.

The Benefits of Automatic Task Completion

The benefits of this script are simple. Your team is no longer tripping over each other when managing deals and data hygiene is pristine.

Create the Python Script to Change Ownership

Here are more detailed step-by-step instructions for the script:

  1. First, import the necessary libraries: requests and os.

    • The requests library is used to send HTTP requests to the HubSpot API. This library is commonly used in almost every Python script.
    • The os library is used to pull the private token securely from the environment. This is an important step in keeping your private key secure.
  2. Create a new function called "main" that receives the "event" object from HubSpot.

    • The event object is passed to the script by HubSpot and contains information about the event that triggered the script to run. In this case, the event is the closing of a deal or ticket.
  3. Within the function, pull the "Private Token" variable from the environment to keep your private key secure using the os.getenv('PRIVATE_TOKEN') command.

    • The private token is a key that grants access to the HubSpot API. It is important to keep this key secure. By using the os.getenv('PRIVATE_TOKEN') command, the script will retrieve the private token from the environment, rather than hardcoding it into the script.
  4. Then, pull the deal owner and deal ID from the input. The variable will be stored as "hs_object_id" and "hubspot_owner_id".

    • The deal ID is used to identify which deal the script should operate on. The event.get('inputFields').get('hs_object_id') command retrieves the deal ID from the input fields of the event object passed to the script.
  5. Create a variable for the url to get the associated tasks of a deal.

    • The url variable is used to make a GET request to the HubSpot API to retrieve the task associations of the ticket. The url is constructed by concatenating the base url, the ticket ID and the task associations endpoint.
  6. Create header variables that give your code access to the API using the headers variable.

    • The headers variable is used to provide the necessary authentication information to the API. It includes a content-type of "application/json" and the private token from step 3 in the format "Authorization: Bearer {private_token}".
  7. Send a GET request to the url variable to get the task associations of the deals using the requests.get(url, headers=headers) command.

    • This GET request retrieves the task associations of the deal specified by the deal ID. The headers variable is passed as an argument to the request to provide the necessary authentication information.
  8. Retrieve the results from the response to your GET request using response.json()['results']

    • The response from the GET request is in JSON format, which can be parsed using the json() method. The ['results'] part of the command is used to access the array of task associations returned in the response.
  9. Pull all the ids from the tasks associated with this deal that have not been completed using a list comprehension

    • The list comprehension iterates through the results array and extracts the task id from each task association, creating a new array of task ids.
    • tasks_not_started = [x['hs_object_id'] for x in all_engagements if x['hs_task_status'] == 'NOT_STARTED']
  10. Create a Task Update URL to update the tasks to complete `task_update_url = "https://api.hubapi.com/crm/v3/objects/tasks/batch/update"

  11. Send the new owner as a property to the update task API

Create the Custom Code Workflow in HubSpot

Next, we need to create our workflow so this code triggers every single time a new ticket is closed.

From the main menu, click on the "Automation" tab and then click on the "Workflows" option.

You will want to click the orange "Create workflow" button in the top right and click on "From scratch". This will create a new blank workflow.

Next, select the "Ticket-based" option on the left-hand column. Select the "Blank workflow" radio button and click the orange "Next button" on the top right.

deal_workflow

Click on the "Trigger" area at the top of the workflow which will open up the trigger options on the righthand side.

deal_trigger

 

Set the trigger for when a deal owner is known. Set this for re-enrollment so it changes every time there's a change.

deal_owner_is_known

Click on the "Custom code" option as an action.

Change the language from the default Javascript to Python.

Add in your Private Token secret. It will typically look like PRIVATE_TOKEN. The code below is setup to use this name so make sure you change out the name if you name your private token different.

Add your deal id as a property to pass into the code as "hs_object_id". This variable is a critical name for the code to work.

object_owners

Finally, copy the script into the workflow code area. You can download the script below.

Next, click on the "Review and publish" orange button in the top right.

Set the workflow to only apply to tickets going forward by selecting the "No" radio button. If you use Yes, it will throw a lot of errors as these custom code pieces aren't designed for backlogging and queueing natively.

Your final workflow should look like this.

Download the Script

Now that you understand how to build it, the next step is putting it in action.

You can download the script below by putting in your email address.

Feel free to reach out if you have any questions.

 

Your CRM Should Multiply Your Company's Success.

Your CRM determines how efficient your revenue team can be.

  • Imagine your sales cycle dropping quarter by quarter
  • Imagine your return on ad spend skyrocketing
  • Imagine your churn dropping without any new feature updates

All of this is possible with a powerful CRM and a partner you can rely on.