0

I am new to external scripting in Service Now.

I have this requirement of performing a text search in Service Now. I'm planning to insert the texts to be searched in a csv file and then through a python rest API of service now, perform the text search.

However, I was unable to find any direct python REST Service Now API to do text search. Does anyone know any alternative ways of doing this task?

Thanks in Advance!

Stack Kiddy
  • 556
  • 1
  • 4
  • 10
Jacky
  • 15
  • 6

1 Answers1

1

You can use 'Global Text Search API' in ServiceNow platform to get search results. First you need to get the list of search groups available and then send a query with list of groups that need to be searched and the search keyword.

Here's a sample program that searched for keyword agent in Knowledge base and Service catalog.

import requests

# Set the request parameters
url = 'https://<instance>.service-now.com/api/now/v1/globalsearch/groups'

# 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)

url = 'https://<instance>.service-now.com/api/now/v1/globalsearch/search?sysparm_groups=2b77ce594bd6c0d901b8fbe25ceff671&sysparm_search=agent'

# 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)
Siri
  • 84
  • 3
  • what does this mean "sysparm_groups=2b77ce594bd6c0d901b8fbe25ceff67" does this represent the ID of the search group – Jacky Nov 15 '19 at 09:47
  • Yes, that's the ID of Knowledge base and Service catalog search group. You can find the IDs of different search groups from first query. – Siri Nov 16 '19 at 10:16