Search code examples
pythonpython-3.xservicenowservicenow-rest-api

Python REST Service Now API for text search


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!


Solution

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