Skip to content

Instantly share code, notes, and snippets.

@dpawluk
Last active October 19, 2016 21:12
Show Gist options
  • Select an option

  • Save dpawluk/74a2a7d816b00649e317de78bdfc0cc5 to your computer and use it in GitHub Desktop.

Select an option

Save dpawluk/74a2a7d816b00649e317de78bdfc0cc5 to your computer and use it in GitHub Desktop.
copy value from 'old_field' to 'new_field' on all users, replace instances of 'old_field' and 'new_field' with the appropriate keys
import json
import requests
import math
config = {
"subdomain": "", ## Your Zendesk subdomain
"admin_user": "", ## An Administrator user's email
"token": "" ## Your Zednesk API token
}
s = requests.Session()
s.auth = ("{}/token".format(config['admin_user']),config['token'])
s.headers.update({"content-type": "application/json"})
def get_users_by_page(page):
url = "https://{}.zendesk.com/api/v2/users.json".format(config['subdomain'])
params = {"page": page}
r = s.get(url, params=params)
if (r.status_code == 200):
response = json.loads(r.text)
return response
else:
print("Page failed to load, error code {}".format(r.status_code))
def update_user(user_id, payload):
url = "https://{}.zendesk.com/api/v2/users/{}.json".format(config['subdomain'], user_id)
r = s.put(url, data=payload)
if (r.status_code == 200):
response = json.loads(r.text)
return response
else:
print("Error modifying user with id: {}, error status code {}".format(user_id, r.status_code))
first_page = get_users_by_page(1)
total_users = first_page['count']
total_pages = math.ceil(total_users/100.00)
print(total_pages)
total_pages = int(total_pages)
for x in range(1, total_pages):
response = get_users_by_page(x)
users = response['users']
for user in users:
new_field_value = str(user['user_fields']['old_field']) ## Wrapped in Str() because it is a numeric field in this case and we are copying it to a text field
payload = {
"user": {
"user_fields": {
"new_field": new_field_value
}
}
}
json_payload = json.dumps(payload)
updated = update_user(user['id'], json_payload)
if updated:
print("Successfuly updated user with id: {}".format(user['id']))
else:
print("Failure *****")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment