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.
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 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 this script are simple. Your team is no longer tripping over each other when managing deals and data hygiene is pristine.
Here are more detailed step-by-step instructions for the script:
First, import the necessary libraries: requests
and os
.
requests
library is used to send HTTP requests to the HubSpot API. This library is commonly used in almost every Python script.os
library is used to pull the private token securely from the environment. This is an important step in keeping your private key secure.Create a new function called "main" that receives the "event" object from HubSpot.
Within the function, pull the "Private Token" variable from the environment to keep your private key secure using the os.getenv('PRIVATE_TOKEN')
command.
os.getenv('PRIVATE_TOKEN')
command, the script will retrieve the private token from the environment, rather than hardcoding it into the script.Then, pull the deal owner and deal ID from the input. The variable will be stored as "hs_object_id" and "hubspot_owner_id".
event.get('inputFields').get('hs_object_id')
command retrieves the deal ID from the input fields of the event object passed to the script.Create a variable for the url to get the associated tasks of a deal.
Create header variables that give your code access to the API using the headers
variable.
Send a GET request to the url
variable to get the task associations of the deals using the requests.get(url, headers=headers)
command.
headers
variable is passed as an argument to the request to provide the necessary authentication information.Retrieve the results from the response to your GET request using response.json()['results']
json()
method. The ['results']
part of the command is used to access the array of task associations returned in the response.Pull all the ids from the tasks associated with this deal that have not been completed using a list comprehension
Create a Task Update URL to update the tasks to complete `task_update_url = "https://api.hubapi.com/crm/v3/objects/tasks/batch/update"
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.
Click on the "Trigger" area at the top of the workflow which will open up the trigger options on the righthand side.
Set the trigger for when a deal owner is known. Set this for re-enrollment so it changes every time there's a change.
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.
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.
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.