- 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
‎12-03-2020 09:21 PM
Hi Ankur,
I have tried the below and haven't succeeded in getting the values -
- Using sysparm_fields=variables
-
{ "result": [ { "variables": "" } ] }
- Using Different RITMs
-
{ "result": [ {} ] }
- Using Different Variables
-
{ "result": [ {} ] }
I have Admin privileges on the user. Please help.
As per my initial script, data in 266 RITM are taking more than 6 hours.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2020 09:36 PM
Hi Shashank,
based on what you say it sounds like some issue with access/some thing restricting. I am not familiar on the setup for RITM and it's variables so can't really comment on that.
If this setup is in your personal instance and you are ok to share details url and some admin credentials then share here -> ankurb.snow@gmail.com
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
‎12-07-2020 11:15 PM
Unfortunately the instance is not personal so could not share more details.
However, I dug down into variables and found out that each variable was tied to a specific key. The value of key was not know to the user I was using but to the team who manages ServiceNow.
Hence after using those keys I am able to directly find the values for now. Not the most elegant way but works for now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2020 11:41 PM
Hi,
Are you saying the name of the variable?
I thought you are using the same
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
‎12-07-2020 11:51 PM
Request URL
https://xxx.service-now.com/api/now/table/sc_req_item?sysparm_query=number%3DRITM5829160&sysparm_display_value=true&sysparm_exclude_reference_link=true&sysparm_fields=cat_item, state, number,variables.6e3e68cc0fb392006096748362050e1
Response
{
"result": [
{
"number": "RITM5829160",
"cat_item": "TEST",
"variables.6e3e68cc0fb392006096748362050e1": null,
"state": "Closed Complete"
}
]
}