Skip to content

Instantly share code, notes, and snippets.

@sbkinney
Created March 1, 2022 15:20
Show Gist options
  • Save sbkinney/558ec5378611389591835185fdcad19c to your computer and use it in GitHub Desktop.
Save sbkinney/558ec5378611389591835185fdcad19c to your computer and use it in GitHub Desktop.
Grab the incident counts from problem tickets in Zendesk and write this data back to the tickets in bulk using a custom ticket field
import json
# Customize these four lines for your Zendesk environment
zd_subdomain = 'subdomain' # the portion before .zendesk.com
zd_user = '[email protected]/token' # Your email address
zd_token = 'adbjhsabfjhbwshfubwhjsfbnsqjfcn' # Your API token
zd_incident_field = 123456789 # Replace this with the ID of the custom ticket field you're using to store the incident count
zd_headers = {'content-type': 'application/json'}
next_page = 'https://' + zd_subdomain + '.zendesk.com/api/v2/problems.json?include=incident_counts&page=1'
results = []
while next_page:
response = requests.get(next_page, auth=(zd_user, zd_token))
tickets = response.json()['tickets']
next_page = response.json()['next_page']
for ticket in tickets:
results.append({ "id": ticket['id'], "custom_fields": [{ "id": zd_incident_field, "value": ticket['incident_count']}]})
# Break into chunks of 100 records since this is the limit on the bulk update endpoint Zendesk offers
chunks = [results[x:x+100] for x in range(0, len(results), 100)]
bulk_response = []
for chunk in chunks:
payload = {"tickets": chunk}
response = requests.put('https://' + zd_subdomain + '.zendesk.com/api/v2/tickets/update_many.json', data=json.dumps(payload), auth=(zd_user, zd_token), headers={'Content-Type': 'application/json'})
bulk_response.append(response.json())
output = {'jobs': bulk_response}
@sbkinney
Copy link
Author

sbkinney commented Mar 1, 2022

A quick and dirty Python script that can be scheduled or triggered (e.g. in Zapier) to sync incident count to Problems to workaround the lack of this data being available in views in their core product.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment