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

eric_hemmer
ServiceNow Employee
ServiceNow Employee

Many customers are using ServiceNow, especially EC Pro, as their intranet page so their employees are presented with a combined experience of targeted communications (e.g. NEWS) and service management.  This way employees never have to contend with the decision about where to go for answers and support - it's all in the same place they start their day.  I have a special interest group for customers who have done this or who are interested in this and it's grown from 30 members three years ago to 920 members today.  If you'd like to adopt this trend, there are many resources here and you can also join our next meeting by registering for the Feb 6th event.  https://www.servicenow.com/community/ex-platform-for-the-modern/gh-p/sig-modern-intranet

Andrew Pickles
Tera Expert

Just wanted everyone to know - i solved the issue (with the help above)

 

The script provided by @Iraj Shaikh DID work, however first i needed to create a new CORS rule in servicenow to allow the cross domain javascript based REST request 🙂

Thanks also to @Maik Skoddow  for giving me an idea what the problem might be