APIs & Integrations

LeonSuperchat
Member

Find Conversation association to Ticket

Hey everyone,
I'm trying to find an association between a ticket and a conversation by using the code below. My included property is the Ticket ID. Unfortunately I get the log that no associations were found. I tested it for multiple tickets that have an associated conversation.

 

import os
import requests

def main(event):
    # Extract dynamic variables from the event object
    hs_ticket_id = event["inputFields"]["hs_ticket_id"]
    
    # Extract the API key or access token from the secrets manager
    HUBSPOT_API_KEY = os.environ.get("Authorization")

    # Construct the URL
    url = f"https://api.hubapi.com/crm/v4/objects/tickets/{hs_ticket_id}/associations/conversations"

    # Set headers, including the API key or access token
    headers = {
        "Content-Type": "application/json",
        "Authorization": HUBSPOT_API_KEY
    }

    # Make the GET request
    response = requests.get(url, headers=headers)

    # Prepare the output fields for HubSpot
    outputFields = {"association_id": None, "error": None}

    # Check the response
    if response.status_code == 200:
        print("Request successful!")
        
        # Parse the response as JSON
        response_data = response.json()
        print("Response Data:", response_data)  # Debugging statement to inspect structure
        
        # Check if associations exist in the response
        associations = response_data.get("associations", {})
        if "pxxxxxxxxx_ticket" in associations:
            ticket_associations = associations["pxxxxxxxxx_ticket"]["results"]
            if ticket_associations:
                first_association = ticket_associations[0]
                association_id = first_association.get("toObjectID")
                
                # Store the association id in outputFields
                outputFields["association_id"] = association_id
                print(f"Association ID: {association_id}")
            else:
                print("No associations found.")
                outputFields["association_id"] = None
        else:
            print("No associations key found in the response.")
            outputFields["association_id"] = None
    else:
        print(f"Failed to retrieve object: {response.status_code} - {response.text}")
        outputFields["error"] = f"Failed with status code {response.status_code}"

    # Return the outputFields dictionary to HubSpot
    return {"outputFields": outputFields}
  

 

0 Upvotes
1 Reply 1
Jaycee_Lewis
Community Manager
Community Manager

Find Conversation association to Ticket

Hey, @LeonSuperchat 👋 I'd suggest you review the endpoint you are attempting to use as a starting point. You have `https://api.hubapi.com/crm/v4/objects/tickets/{hs_ticket_id}/associations/conversations` in your example, but the current versions are v3 and v1:

CleanShot 2024-11-12 at 16.31.06@2x.png

 

Before you work with your custom code, you might set up a test using Postman. Once successful, you can generate a code example directly in Postman and use that snippet to work on your code. You can also test from the example page to make sure your basic requests are working as expected.

 

Best,

Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes