How do I retreive Variables in RITM via Table API and Python

Shashank7
Kilo Explorer

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. find_real_file.png

The way I perform everything in this script is - 

  1. Use the RITM number of the ticket and call the API below which gives me the list as a response. 
  2. "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"
  3. Then I poll every item of this list and fetch sc_item_option key.
  4. For every sc_item_option_key found, I use this key to call another API as below.
  5. "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"
  6. 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. 

1 ACCEPTED SOLUTION

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:

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

18 REPLIES 18

Is that variable alias a multi row variable set or plain variable

A. How do I check that ?

On the UI it shows as a free text

find_real_file.png

 

Did you check the variable has some value in it

A. Yes.

Hi,

with which user you are consuming the endpoint

try with admin and see if value is getting fetched

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The user I am using has admin privileges.

Hi,

did you try some other RITM and variable

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi,

I tried with other RITM

 

For variables with whitespaces, should I replace the space with underscore ?