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

Hi Ankur,

 

 

I have tried the below and haven't succeeded in getting the values - 

  1. Using sysparm_fields=variables
  2. {
        "result": [
            {
                "variables": ""
            }
        ]
    }
  3. Using Different RITMs
  4. {
        "result": [
            {}
        ]
    }
  5. Using Different Variables
  6. {
        "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.

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

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

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.

Hi,

Are you saying the name of the variable?

I thought you are using the same

Regards
Ankur

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

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"
        }
    ]
}