Sign in

Assign a Ticket to a User in Zendesk

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This is a task where a specific Zendesk ticket is allocated to a designated user or agent for resolution, ensuring targeted and efficient handling of customer queries or issues.

import zenpy from zenpy.lib.exception import ZenpyException, RecordNotFoundException, APIException # Credentials for Zendesk API creds = _get_creds(cred_label)['creds'] ZENDESK_EMAIL = creds['username'] ZENDESK_TOKEN = creds['password'] #ZENDESK_SUBDOMAIN = 'your-subdomain' # Initialize Zenpy client client = zenpy.Zenpy(email=ZENDESK_EMAIL, token=ZENDESK_TOKEN, subdomain=ZENDESK_SUBDOMAIN) def assign_ticket_to_user(ticket_id, assignee_id, priority='normal'): """ Assigns a ticket to a specified user in Zendesk and sets the priority, if needed. Args: ticket_id (int): The ID of the ticket to be assigned. assignee_id (int): The ID of the user to whom the ticket will be assigned. priority (str): The priority of the ticket ('urgent', 'high', 'normal', 'low'). The function verifies the existence of the ticket and the user before proceeding with the assignment. If the ticket is already assigned to the specified user with the same priority, it informs the user. """ try: # Retrieve the ticket by its ID ticket = client.tickets(id=ticket_id) if not ticket: print(f"Ticket with ID {ticket_id} does not exist.") return # Check if the user exists and is active in Zendesk user = client.users(id=assignee_id) if not user or user.active is False: print(f"User with ID {assignee_id} does not exist or is inactive.") return # Check if the ticket is already assigned with the same priority if ticket.assignee_id == assignee_id and ticket.priority == priority: print(f"Ticket {ticket_id} is already assigned to user {assignee_id} with priority {priority}. No changes made.") return # Assign the ticket to the user and set the priority ticket.assignee_id = assignee_id ticket.priority = priority client.tickets.update(ticket) print(f"Ticket {ticket_id} has been assigned to user {assignee_id} with priority {priority}.") except RecordNotFoundException: print(f"Ticket ID {ticket_id} or User ID {assignee_id} does not exist in Zendesk.") except APIException as api_ex: print(f"APIException occurred: {api_ex}") except ZenpyException as zen_ex: print(f"ZenpyException occurred: {zen_ex}") except Exception as e: print(f"An unexpected error occurred: {e}") # Example Usage # Replace the ticket ID and user ID with valid values #TICKET_ID_TO_UPDATE = 3 #user_id = 16645863103762 assign_ticket_to_user(TICKET_ID_TO_UPDATE, user_id, priority='high')
copied