Search code examples
pythonapizendeskzendesk-api

Extracting the Name of Zendesk Custom Fields through APIs in Python


I'm trying to extract the custom field names created by me (who is also an admin in zendesk) through API calls in Zendesk with Python but so far have only been successful in extracting the IDs of the custom fields. Is there anyway to extract the name of the custom fields so I know what to populate it with. here is my code ,


credentials = {'token': ' ', 'email':'', 'subdomain': ' '}

create_ticket = zenpy_client.tickets.create(
    Ticket(subject='This field will contain a subject',
           description='This field will contain description',
           requester=User(name='testtest'), assignee_id=5883758502685, priority='normal'
           )
)

ticket_id = create_ticket.__dict__['ticket'].id


custom_field_ticket = zenpy_client.tickets(id=ticket_id)

custom_fields = custom_field_ticket.__dict__['custom_fields'] # this provides a list of IDs as list of dictionaries


Can anyone please provide some insight ?


Solution

  • The only end point that will expose field title is ticket_fields.

    So you can create a dictionary of field titles as keys and ids as values for readable code while updating custom fields:

    ticket_fields = {}
    for ticket_field in zenpy_client.ticket_fields():
        ticket_fields[ticket_field.title] = ticket_field.id
    

    Here is a full example of how to create a ticket and set its custom fields using titles instead of ids:

    from zenpy import Zenpy
    from zenpy.lib.api import Ticket, User
    from os import environ
    
    creds = {
        "email": environ["EMAIL"],
        "token": environ["TOKEN"],
        "subdomain": environ["SUBDOMAIN"],
    }
    zenpy_client = Zenpy(**creds)
    
    ticket_fields = {}
    for ticket_field in zenpy_client.ticket_fields():
        ticket_fields[ticket_field.title] = ticket_field.id
    
    new_ticket = Ticket(
        subject="Ticket Subject",
        description="Ticket description",
        requester=User(name="John Smith", email="[email protected]"),
        fields=[{"id": ticket_fields["My Custom Field"], "value": "my_value"}],
    )
    zenpy_client.tickets.create(new_ticket)