I am able to fetch the Incident details via APi in Python but I am not able to fetch the SLA percentage on that incident via Service now API. Any help would by highly appreciated?

Jeya
Tera Contributor

I am able to fetch the incident details via servicenow incident api but couldn’t get the sla percentage of that particular incident . Is there any service now api where we can get the incident sla percentage returned along with other incident details ( status, assigned , etc ). Please suggest 

1 ACCEPTED SOLUTION

mak1A4
Tera Guru

Hello Jeya, I think you should query the task_sla table with the "now Table API" here is a sample python script from REST API Explorer:

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

# Set the request parameters
url = 'https://yourInstance.service-now.com/api/now/table/task_sla?sysparm_query=task%3D97114c1cdbb2fc10ecec1a4813961921&sysparm_display_value=all&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)

This should return the following data:

{
  "result": [
    {
      "pause_duration": {
        "display_value": "1 Minute",
        "value": "1970-01-01 00:01:00"
      },
      "pause_time": {
        "display_value": "",
        "value": ""
      },
      "timezone": {
        "display_value": "Europe/Amsterdam",
        "value": "Europe/Amsterdam"
      },
      "sys_updated_on": {
        "display_value": "22.09.2021 08:41:35",
        "value": "2021-09-22 06:41:35"
      },
      "business_time_left": {
        "display_value": "0 Seconds",
        "value": "1970-01-01 00:00:00"
      },
      "duration": {
        "display_value": "1 Day 19 Hours 32 Minutes",
        "value": "1970-01-02 19:32:33"
      },
      "sys_id": {
        "display_value": "49e20c9cdbb2fc10ecec1a4813961934",
        "value": "49e20c9cdbb2fc10ecec1a4813961934"
      },
      "time_left": {
        "display_value": "0 Seconds",
        "value": "1970-01-01 00:00:00"
      },
      "sys_updated_by": {
        "display_value": "admin",
        "value": "admin"
      },
      "sys_created_on": {
        "display_value": "20.09.2021 13:15:41",
        "value": "2021-09-20 11:15:41"
      },
      "percentage": {
        "display_value": "1,088.56",
        "value": "1088.56"
      },
      "original_breach_time": {
        "display_value": "20.09.2021 17:08:02",
        "value": "2021-09-20 15:08:02"
      },
      "sys_created_by": {
        "display_value": "admin",
        "value": "admin"
      },
      "business_percentage": {
        "display_value": "1,088.57",
        "value": "1088.57"
      },
      "end_time": {
        "display_value": "",
        "value": ""
      },
      "sys_mod_count": {
        "display_value": "38",
        "value": "38"
      },
      "active": {
        "display_value": "true",
        "value": "true"
      },
      "business_pause_duration": {
        "display_value": "1 Minute",
        "value": "1970-01-01 00:01:00"
      },
      "sla": {
        "display_value": "Incident OnHold Test",
        "link": "https://yourInstance.service-now.com/api/now/table/contract_sla/e643d084dbf6bc10ecec1a4813961971",
        "value": "e643d084dbf6bc10ecec1a4813961971"
      },
      "sys_tags": {
        "display_value": "",
        "value": ""
      },
      "schedule": {
        "display_value": "24 x 7",
        "link": "https://yourInstance.service-now.com/api/now/table/cmn_schedule/38fa64edc0a8016400f4a5724b0434b8",
        "value": "38fa64edc0a8016400f4a5724b0434b8"
      },
      "start_time": {
        "display_value": "20.09.2021 13:08:02",
        "value": "2021-09-20 11:08:02"
      },
      "business_duration": {
        "display_value": "1 Day 19 Hours 32 Minutes",
        "value": "1970-01-02 19:32:33"
      },
      "task": {
        "display_value": "INC0010067",
        "link": "https://yourInstance.service-now.com/api/now/table/task/97114c1cdbb2fc10ecec1a4813961921",
        "value": "97114c1cdbb2fc10ecec1a4813961921"
      },
      "stage": {
        "display_value": "In progress",
        "value": "in_progress"
      },
      "planned_end_time": {
        "display_value": "20.09.2021 17:09:02",
        "value": "2021-09-20 15:09:02"
      },
      "has_breached": {
        "display_value": "true",
        "value": "true"
      }
    }
  ]
}

so there you have access to the fields "business_percentage" and "percentage". Hope that helps.

View solution in original post

7 REPLIES 7

mak1A4
Tera Guru

Hello Jeya, I think you should query the task_sla table with the "now Table API" here is a sample python script from REST API Explorer:

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

# Set the request parameters
url = 'https://yourInstance.service-now.com/api/now/table/task_sla?sysparm_query=task%3D97114c1cdbb2fc10ecec1a4813961921&sysparm_display_value=all&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)

This should return the following data:

{
  "result": [
    {
      "pause_duration": {
        "display_value": "1 Minute",
        "value": "1970-01-01 00:01:00"
      },
      "pause_time": {
        "display_value": "",
        "value": ""
      },
      "timezone": {
        "display_value": "Europe/Amsterdam",
        "value": "Europe/Amsterdam"
      },
      "sys_updated_on": {
        "display_value": "22.09.2021 08:41:35",
        "value": "2021-09-22 06:41:35"
      },
      "business_time_left": {
        "display_value": "0 Seconds",
        "value": "1970-01-01 00:00:00"
      },
      "duration": {
        "display_value": "1 Day 19 Hours 32 Minutes",
        "value": "1970-01-02 19:32:33"
      },
      "sys_id": {
        "display_value": "49e20c9cdbb2fc10ecec1a4813961934",
        "value": "49e20c9cdbb2fc10ecec1a4813961934"
      },
      "time_left": {
        "display_value": "0 Seconds",
        "value": "1970-01-01 00:00:00"
      },
      "sys_updated_by": {
        "display_value": "admin",
        "value": "admin"
      },
      "sys_created_on": {
        "display_value": "20.09.2021 13:15:41",
        "value": "2021-09-20 11:15:41"
      },
      "percentage": {
        "display_value": "1,088.56",
        "value": "1088.56"
      },
      "original_breach_time": {
        "display_value": "20.09.2021 17:08:02",
        "value": "2021-09-20 15:08:02"
      },
      "sys_created_by": {
        "display_value": "admin",
        "value": "admin"
      },
      "business_percentage": {
        "display_value": "1,088.57",
        "value": "1088.57"
      },
      "end_time": {
        "display_value": "",
        "value": ""
      },
      "sys_mod_count": {
        "display_value": "38",
        "value": "38"
      },
      "active": {
        "display_value": "true",
        "value": "true"
      },
      "business_pause_duration": {
        "display_value": "1 Minute",
        "value": "1970-01-01 00:01:00"
      },
      "sla": {
        "display_value": "Incident OnHold Test",
        "link": "https://yourInstance.service-now.com/api/now/table/contract_sla/e643d084dbf6bc10ecec1a4813961971",
        "value": "e643d084dbf6bc10ecec1a4813961971"
      },
      "sys_tags": {
        "display_value": "",
        "value": ""
      },
      "schedule": {
        "display_value": "24 x 7",
        "link": "https://yourInstance.service-now.com/api/now/table/cmn_schedule/38fa64edc0a8016400f4a5724b0434b8",
        "value": "38fa64edc0a8016400f4a5724b0434b8"
      },
      "start_time": {
        "display_value": "20.09.2021 13:08:02",
        "value": "2021-09-20 11:08:02"
      },
      "business_duration": {
        "display_value": "1 Day 19 Hours 32 Minutes",
        "value": "1970-01-02 19:32:33"
      },
      "task": {
        "display_value": "INC0010067",
        "link": "https://yourInstance.service-now.com/api/now/table/task/97114c1cdbb2fc10ecec1a4813961921",
        "value": "97114c1cdbb2fc10ecec1a4813961921"
      },
      "stage": {
        "display_value": "In progress",
        "value": "in_progress"
      },
      "planned_end_time": {
        "display_value": "20.09.2021 17:09:02",
        "value": "2021-09-20 15:09:02"
      },
      "has_breached": {
        "display_value": "true",
        "value": "true"
      }
    }
  ]
}

so there you have access to the fields "business_percentage" and "percentage". Hope that helps.

Jeya
Tera Contributor

Thank you for your response. This API dont have the incident number, how can i relate the incident number with this API ? Can you please help here?

if you don't have the sys_id of the incident you can change the encoded query to

incidentNumber = 'INC000001'
url = 'https://yourInstance.service-now.com/api/now/table/task_sla?sysparm_query=task.number=' + incidentNumber + '&sysparm_display_value=all'

Jeya
Tera Contributor

Thank you so much. Awesome. it works