Skip to content

Instantly share code, notes, and snippets.

@marksie1988
Created February 4, 2021 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marksie1988/a8aefed68cef72ae2ce9202fb4677ca7 to your computer and use it in GitHub Desktop.
Save marksie1988/a8aefed68cef72ae2ce9202fb4677ca7 to your computer and use it in GitHub Desktop.
Script to get and update records in ServiceNow
# Need to install requests package for python
# easy_install requests
import requests
sn_username = ''
sn_password = ''
# this is the beginning of your URL e.g. company.service-now.com would be "company"
instance_name = ''
# The table to pull data and update records
table_name = ''
# the sysparam Query to select the records you wish to updated
query_url = ''
# A list of attributes for your table can be found in the API Explorer
# These values will differ depending on the table
payload = {
'install_status': 'Retired'
}
################################
#### DO NOT EDIT BELOW HERE ####
################################
get_url = f"https://{instance_name}.service-now.com/api/now/table/{table_name}{query_url}"
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
response = requests.get(get_url, auth=(sn_username, sn_password), headers=headers )
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = response.json()
# Loop through returned results and update with payload
for device in data['result']:
device_id = device['sys_id']
patch_url = f"https://{instance_name}.service-now.com/api/now/table/{table_name}/{device_id}"
# Save the result of the request in a variable
result = requests.patch(patch_url,
json=payload,
headers=headers,
auth=(sn_username, sn_password))
# print the sys_id and result
print(f"sys_id: {device['sys_id']}, Result: {result},")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment