0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ServiceNow】APIからCatalog Itemの設定を取得したい

Last updated at Posted at 2022-04-17

はじめに

  • バージョン
    • ServiceNow: Rome
    • Python: 3.8.8

Catalog Itemの設定にはその配下にvariablesやCatalog UI Policiesなど、
複数あります。今回は、REST API Explorerを使ってCatalog Item(Name: api_test)と
これに紐づくvariablesの設定を取得してみます。

catalog_item.JPG

1. Catalog Itemの設定を取得するコードの作成

  1. アプリケーションナビゲーターからREST API Explorerを選択します

  2. Path parametersに以下の設定を入れます

    • table Name: sc_cat_item
    • sysparm_query: name=api_test
      • nameの値がapi_testであるものを検索

    sc_cat_item.JPG

  3. Sendを選択してCatalog Itemの設定を取得出来ていたら、Code Samples内のPython
    選択してコードを作成します

    code_sample.JPG

  4. 以下のようなコードが作成されました

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://devXXXXXX.service-now.com/api/now/table/sc_cat_item?sysparm_query=name%3Dapi_test&sysparm_limit=1'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )

# Check for HTTP codes other than 200
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

2. variablesの設定を取得するコードの作成

Catalog Itemとvariablesの設定を比較すると、variablesのcat_itemという項目に
Catalog Itemのsys_idが入っているようなので、APIで取得します

  1. アプリケーションナビゲーターからREST API Explorerを選択します

  2. Path parametersに以下の設定を入れます

    • table Name: item_option_new
    • sysparm_query: cat_item={{ Catalog Itemのsys_id }}
      • cat_itemの値がCatalog Itemのsys_idと等しいものを検索
      • (注) 列cat_itemはGUIだと見えない

    item_option_new.JPG

  3. Sendを選択してvariablesの設定を取得出来ていたら、Code Samples内のPython
    選択してコードを作成します

  4. 以下のようなコードが作成されました

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://devXXXXXX.service-now.com/api/now/table/item_option_new?sysparm_query=cat_item%3Dxxxxxxxxxxxxx&sysparm_limit=1'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )

# Check for HTTP codes other than 200
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

3. コードの修正

コードの修正をするとキリがないので、最低限の修正を加えました。
主な修正点は以下です。

  • APIへのアクセスに使用する情報を、変数common_infoに記載
  • urlの最後のsysparm_limit=1を削除
    • 取得するデータ数に制限がかかってしまうため
  • Catalog Itemの設定を取得する関数get_catalog_item_settingsと
    variablesの設定を取得する関数get_variables_settingsを作成
    • 引数: クエリに使用する文字列
    • 戻り値: APIの結果
from typing import Dict, List
import requests

common_info = {
    "base_url": "https://devXXXXXX.service-now.com/api/now/table/",
    "api_auth": ("admin", "admin"),
    "api_headers": {
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
}


def get_catalog_item_settings(catalog_item_name: str) -> Dict:
    # Set the request parameters
    url = (
        common_info["base_url"]
        + "sc_cat_item?sysparm_query=name%3D"
        + catalog_item_name
    )
    # Do the HTTP request
    response = requests.get(
        url, auth=common_info["api_auth"], headers=common_info["api_headers"]
    )

    # Check for HTTP codes other than 200
    if response.status_code != 200:
        print(
            "Status:",
            response.status_code,
            "Headers:",
            response.headers,
            "Error Response:",
            response.json(),
        )
        exit()

    # Decode the JSON response into a dictionary and use the data
    data = response.json()
    return data


def get_variables_settings(catalog_item_sys_id: str) -> List:
    # Set the request parameters
    url = (
        common_info["base_url"]
        + "item_option_new?sysparm_query=cat_item%3D"
        + catalog_item_sys_id
    )
    # Do the HTTP request
    response = requests.get(
        url, auth=common_info["api_auth"], headers=common_info["api_headers"]
    )

    # Check for HTTP codes other than 200
    if response.status_code != 200:
        print(
            "Status:",
            response.status_code,
            "Headers:",
            response.headers,
            "Error Response:",
            response.json(),
        )
        exit()

    # Decode the JSON response into a dictionary and use the data
    data = response.json()
    return data


if __name__ == "__main__":
    catalog_item = get_catalog_item_settings("api_test")["result"][0]
    variables = get_variables_settings(catalog_item["sys_id"])["result"]
    print(catalog_item)
    print(variables)

4. 結果の確認

  • Catalog Item
{
    ...,
    "description": "<p>api_test_description</p>",
    "name": "api_test",
    "short_description": "api_test_short_description",
    "sys_id": "xxxxCatalog Itemのsys_idxxxx",
    ...,
}
  • variables
[
    {
        "cat_item": {
            "link": "https://devXXXXXX.service-now.com/api/now/table/sc_cat_item/xxxxCatalog Itemのsys_idxxxx",
            "value": "xxxxCatalog Itemのsys_idxxxx"
        },
        ...,
        "name": "api_test_var1",
        "order": "100",
        "question_text": "api_test_var1",
        ...,
    },
    {
        ...,
        "cat_item": {
            "link": "https://devXXXXXX.service-now.com/api/now/table/sc_cat_item/xxxxCatalog Itemのsys_idxxxx",
            "value": "xxxxCatalog Itemのsys_idxxxx"
        },
        "name": "api_test_var2",
        "order": "200",
        "question_text": "api_test_var2",
        ...,
    }
]

まとめ

Catalog UI Policiesなどvariables以外の値も取りたい場合は、テーブルの中のどの列に
Catalog Itemのsys_idが格納されているかを確認し、因果関係を確認して下さい。

参考記事

Service NowでTable APIを使ってみる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?