- 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 02:06 AM
So I tried another way from one of the answer here - https://community.servicenow.com/community?id=community_question&sys_id=bb9b98db1b851810ada243f6fe4bcb15
New Code -
import requests
url = "https://xxxx.service-now.com/api/now/table/sc_req_item"
querystring = {"sysparm_query":"number%3DRITM5462171","sysparm_display_value":"true"}
payload = ""
headers = {
'Accept': "application/json",
'Authorization': "Basic asdasd=",
'cache-control': "no-cache",
'Postman-Token': "c293d8be-8230-462e-8b4a-87d229cb3e48"
}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
print(response.text)
The response does not have variables or variables.group.
- 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 07:21 PM
GET
URL:
api/now/table/sc_req_item?sysparm_query=number%3DRITM5462171&sysparm_display_value=true&sysparm_exclude_reference_link=true&sysparm_fields=variables.alias
NOTE: Alias is one of the variables
Output:
Content-Encoding →gzip
X-Is-Logged-In →true
X-Transaction-ID →e715b06cdbb8
X-Total-Count →1
Pragma →no-store,no-cache
Cache-control →no-cache,no-store,must-revalidate,max-age=-1
Expires →0
Content-Type →application/json;charset=UTF-8
Transfer-Encoding →chunked
Date →Fri, 27 Nov 2020 03:16:54 GMT
Server →ServiceNow
Set-Cookie →JSESSIONID=DCDA8BFA93B4C043E65C3C6386479A4E; Path=/; HttpOnly; SameSite=None; Secure
Set-Cookie →glide_user=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly; SameSite=None; Secure
Set-Cookie →glide_user_session=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly; SameSite=None; Secure
Set-Cookie →glide_user_route=glide.51767e0dbb70e02ff3dfde9b0ff6cad1; Max-Age=2147483647; Expires=Wed, 15-Dec-2088 06:31:01 GMT; Path=/; HttpOnly; SameSite=None; Secure
Set-Cookie →glide_session_store=A315B06CDBB864988FD6C20F3B961960; Max-Age=1800; Expires=Fri, 27-Nov-2020 03:46:54 GMT; Path=/; HttpOnly; SameSite=None; Secure
Strict-Transport-Security →max-age=63072000; includeSubDomains
Response body:
{
"result": [
{}
]
}
I have tried with variables.my_yes_no and the result is the same.
Please help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-26-2020 07:37 PM
Hi,
my_yes_no is the variable in my RITM
Is that variable alias a multi row variable set or plain variable
It should give the data
Did you check the variable has some value in it
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader