I am trying to rename a couple tags in hundreds of organizations. Knowing that there is no easy API for this job, is the easiest way to handle this is to add a new tag to all of these organizations and then delete the old tag?
I have the following python script prepared with issue finding the organizations with the tag:
#!/usr/bin/python
import sys
import json
import requests
import getpass
sys.stderr.write("Zendesk email: ")
zd_username = raw_input()
zd_password = getpass.getpass('Zendesk password: ')
orgIds = set()
url = "https://mycompany.zendesk.com/api/v2/organizations.json"
while url:
r = requests.get(url, auth=(zd_username, zd_password))
if r.status_code != 200:
print ("Error: login failed")
exit(1)
print("Reading %s ..." % url)
respond = r.json()
for org in respond["organizations"]:
if org['tags'] == 'old_tag':
orgIds.add(org["id"])
url = respond["next_page"]
for id in orgIds:
print("Updating %d..." % id);
r = requests.put("https://mycompany.zendesk.com/api/v2/organizations/%d/tags.json" % id, auth=(zd_username, zd_password),
headers={'Content-type': 'application/json'},
data=json.dumps({"tags" : ["new_tag"]}))
print(r.status_code, r.reason)
Any help is appreciated.
Hi Clive,
While I can't speak to the exact code you need to use, I think this could be made potentially more efficient by using the Update Many Organizations API endpoint. Note the section on Batch update, since you'll be making different updates to each organization that you'll be updating. The data structure you'll need to build as input will include the organization id and the new list of tags for that organization (built by retrieving the existing tags list, removing the old one using string manipulation, and appending the new one).
As will all batch operations, I'd highly recommend that you test this either in a sandbox or by creating a few test organizations and running a test version of your script that can only affect those, and not the rest of your actual customer organizations.