displaying REST reponse count on a webpage ??

Andrew Pickles
Tera Expert

Hi guys , a little help please ..

So we are using the REST aggregate API to retrieve a count of incidents open.

We have an intranet page that we want to display this number on but im struggling on how to get the number from the response so that i can use it elsewhere in the HTML of the intranet page ..

for example :

 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
var requestBody = "";

 

var client=new XMLHttpRequest();

client.open("get",https://******.service-now.com/api/now/stats/incident?sysparm_query=active%3Dtrue%5Eassignment_group%3Dd0bda5c0dba2978092a73a4d7c9619cd&sysparm_count=true);

 

client.setRequestHeader('Accept','application/json');

client.setRequestHeader('Content-Type','application/json');

 

//Eg. UserName="admin", Password="admin" for this code sample.

client.setRequestHeader('Authorization', 'Basic '+btoa('username'+':'+'password'));

 

client.onreadystatechange = function() {

if(this.readyState == this.DONE) {

document.getElementById("response").innerHTML=this.status + this.response;

}

};

client.send(requestBody);


</script>
</head>
<body>

I want to use the count data here !!
</body>
</html>

 

Can anyone help on how to actually access the response please ??

 

Many thanks

1 ACCEPTED SOLUTION

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Andrew Pickles 

 

To access the count of open incidents from the REST API response and display it on your intranet page, you'll need to parse the JSON response and extract the count value. Here's how you can modify your JavaScript code to achieve this:

 

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var requestBody = "";

var client = new XMLHttpRequest();

client.open("get", "https://******.service-now.com/api/now/stats/incident?sysparm_query=active%3Dtrue%5Eassignment_group%3Dd0bda5c0dba2978092a73a4d7c9619cd&sysparm_count=true");

client.setRequestHeader('Accept', 'application/json');
client.setRequestHeader('Content-Type', 'application/json');

//Eg. UserName="admin", Password="admin" for this code sample.
client.setRequestHeader('Authorization', 'Basic ' + btoa('username' + ':' + 'password'));

client.onreadystatechange = function() {
    if (this.readyState == this.DONE) {
        if (this.status == 200) {
            // Parse the JSON response
            var responseData = JSON.parse(this.response);
            // Extract the count from the response
            var count = responseData.result.stats.count;
            // Display the count in the desired HTML element
            document.getElementById("incidentCount").innerHTML = count;
        } else {
            // Handle errors, if any
            document.getElementById("incidentCount").innerHTML = "Error retrieving data";
        }
    }
};

client.send(requestBody);
</script>
</head>
<body>

<div id="incidentCount">Loading incident count...</div>

</body>
</html>

 


Here's what the code does:

1. It sends a GET request to the ServiceNow REST API endpoint to retrieve the count of open incidents.
2. Once the request is done (`this.readyState == this.DONE`), it checks if the request was successful (`this.status == 200`).
3. If successful, it parses the JSON response using `JSON.parse(this.response)`.
4. It then extracts the count from the parsed JSON object using `responseData.result.stats.count`.
5. Finally, it sets the innerHTML of an HTML element with the ID `incidentCount` to display the count.


Make sure to replace the placeholder URL with your actual ServiceNow instance URL and update the `username` and `password` with your actual credentials.


Also, ensure that the element where you want to display the count has the correct ID. In this example, I've used `<div id="incidentCount">Loading incident count...</div>` to display the count. Adjust the ID as needed to match your HTML structure.

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

6 REPLIES 6

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Andrew Pickles 

 

To access the count of open incidents from the REST API response and display it on your intranet page, you'll need to parse the JSON response and extract the count value. Here's how you can modify your JavaScript code to achieve this:

 

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var requestBody = "";

var client = new XMLHttpRequest();

client.open("get", "https://******.service-now.com/api/now/stats/incident?sysparm_query=active%3Dtrue%5Eassignment_group%3Dd0bda5c0dba2978092a73a4d7c9619cd&sysparm_count=true");

client.setRequestHeader('Accept', 'application/json');
client.setRequestHeader('Content-Type', 'application/json');

//Eg. UserName="admin", Password="admin" for this code sample.
client.setRequestHeader('Authorization', 'Basic ' + btoa('username' + ':' + 'password'));

client.onreadystatechange = function() {
    if (this.readyState == this.DONE) {
        if (this.status == 200) {
            // Parse the JSON response
            var responseData = JSON.parse(this.response);
            // Extract the count from the response
            var count = responseData.result.stats.count;
            // Display the count in the desired HTML element
            document.getElementById("incidentCount").innerHTML = count;
        } else {
            // Handle errors, if any
            document.getElementById("incidentCount").innerHTML = "Error retrieving data";
        }
    }
};

client.send(requestBody);
</script>
</head>
<body>

<div id="incidentCount">Loading incident count...</div>

</body>
</html>

 


Here's what the code does:

1. It sends a GET request to the ServiceNow REST API endpoint to retrieve the count of open incidents.
2. Once the request is done (`this.readyState == this.DONE`), it checks if the request was successful (`this.status == 200`).
3. If successful, it parses the JSON response using `JSON.parse(this.response)`.
4. It then extracts the count from the parsed JSON object using `responseData.result.stats.count`.
5. Finally, it sets the innerHTML of an HTML element with the ID `incidentCount` to display the count.


Make sure to replace the placeholder URL with your actual ServiceNow instance URL and update the `username` and `password` with your actual credentials.


Also, ensure that the element where you want to display the count has the correct ID. In this example, I've used `<div id="incidentCount">Loading incident count...</div>` to display the count. Adjust the ID as needed to match your HTML structure.

Please mark this response as correct or helpful if it assisted you with your question.

Thank you so much for your response ! 😄

However, although the exact code was working in the REST API explorer and generating the correct results, when i try your code i get "Error retrieving data" ..

Have you any idea why that might be ??

 

Thanks again

Hi @Andrew Pickles 

the above example doesn't work due to security restrictions in browsers which prevents retrieving data from another domain. Instead you have to fetch the data on server-side. An example is given on https://www.servicenow.com/community/developer-articles/3rd-party-integrations-into-service-portal-p... 

Maik

Andrew Pickles
Tera Expert

I dont understand . Surely the whole point of REST is to retrieve data from ServiceNow.

We integrated with a different 3rd party by just providing them URL's to retrieve data from our ServiceNow instance via REST .. That worked, so why wouldnt this work ?