- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 12:42 AM
Version Details
Build name: Newyork
Build date: 08-05-2020_0901
Build tag: glide-newyork-06-26-2019__patch10-07-30-2020
MID buildstamp: newyork-06-26-2019__patch10-07-30-2020_08-05-2020_0901
Question
is there a better way to fetch fieldname and values in the fields of RITM variables ?
P.S - my approach is elaborated below.
Purpose
I have created a python script which fetches the variables as shown below in an RITM ticket.
The way I perform everything in this script is -
- Use the RITM number of the ticket and call the API below which gives me the list as a response.
-
"https://instance_name.service-now.com/api/now/table/sc_item_option_mtom?sysparm_display_value=true&sysparm_exclude_reference_link=True&sysparm_query=request_item%3DRITM_NUMBER&sysparm_fields=sc_item_option"
- Then I poll every item of this list and fetch sc_item_option key.
- For every sc_item_option_key found, I use this key to call another API as below.
- "https://instance_name.service-now.com/api/now/table/sc_item_option?sysparm_limit=10&sysparm_display_value=True&sysparm_exclude_reference_link=True&sysparm_query=sys_id%3Dsc_item_option_key&sysparm_fields=item_option_new,value,description"
- The response of the above API has a fieldname and value of variables in the result.
Code to support above
import requests
import json
#Get ID of each variable by passing the RITM number in the first request
ritm = 'xxx'
user = 'xxx'
pwd = 'xxx'
headers = {"Accept": "application/json"}
mtom_link = "https://xxx.service-now.com/api/now/table/sc_item_option_mtom?sysparm_display_value=true&sysparm_exclude_reference_link=True&sysparm_query=request_item%3D" + str(
ritm) + "&sysparm_fields=sc_item_option"
mtomresponse = requests.get(mtom_link, auth=(user, pwd), headers=headers)
mjsondata = mtomresponse.text
loadmjsondata = json.loads(mjsondata)
mdata = loadmjsondata['result']
mtomkeys = []
# Poll and collect each key
for m in mdata:
mtom_value = m['sc_item_option']
mtomkeys.append(mtom_value)
# For each key call API to retreive fieldnames and values
for key in mtomkeys:
key_link = "https://xxxx.service-now.com/api/now/table/sc_item_option?sysparm_limit=10&sysparm_display_value=True&sysparm_exclude_reference_link=True&sysparm_query=sys_id%3D" + str(
key) + "&sysparm_fields=item_option_new,value,description"
mtomkeyresponse = requests.get(key_link, auth=(user, pwd), headers=headers)
if mtomkeyresponse.status_code != 200:
print('Status:', mtomkeyresponse.status_code, 'Headers:', mtomkeyresponse.headers, 'Error Response:', mtomkeyresponse.content)
exit()
kjsonload = mtomkeyresponse.json()
result = kjsonload['result']
for vn in result:
varname = vn['item_option_new']
value = vn['value']
if varname and varname not in ["Patching information"]:
print("{} => {} : {}".format(key, varname, value)) #Print to view the result
Reason for the question
There are more than 40 variables and more than 100 RITM that I need to fetch details of. Effectively, I will have to make 4000 request calls, which is not effecient.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 02:28 AM
Hi,
you can retrieve variable value as well
give sysparm_fields=variables.my_yes_no
GET
URL: api/now/table/sc_req_item?sysparm_query=number%3DRITM0010425&sysparm_display_value=true&sysparm_exclude_reference_link=true&sysparm_fields=variables.my_yes_no
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 08:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 09:49 PM
Hi,
with which user you are consuming the endpoint
try with admin and see if value is getting fetched
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 11:31 PM
The user I am using has admin privileges.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 11:49 PM
Hi,
did you try some other RITM and variable
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2020 06:32 PM
Hi,
I tried with other RITM
For variables with whitespaces, should I replace the space with underscore ?