display data on portal dyanmically

Community Alums
Not applicable

We are doing servicenow to servicenow integration ,here we want to dynamically display the data when send through rest API from one instance to another on servicenow portal without storing in any table.

1 ACCEPTED SOLUTION

Syedmd08
Kilo Guru

To dynamically display data sent through REST API from one ServiceNow instance to another without storing it in any table, you can use the following approach:

  1. Create a ServiceNow Portal widget that will be used to display the data received from the REST API.

  2. Use a ServiceNow Script Include to make the REST API call to the other instance and retrieve the data. The Script Include can then return the data as a JSON object.

  3. In the widget's client script, use the GlideAjax class to call the Script Include and retrieve the data. You can then use JavaScript to parse the JSON object and dynamically display the data in the widget.

Here's an example of what the Script Include might look like:

 

var RestAPI = Class.create();

RestAPI.prototype = {
initialize: function() {
// Set up REST API options
this.options = {
url: '<REST API URL>',
method: 'GET',
headers: {
'Accept': 'application/json'
}
};
},

getData: function() {
// Make REST API call and return JSON object
var response = new sn_ws.RESTMessageV2();
response.setEndpoint(this.options.url);
response.setHttpMethod(this.options.method);
response.setRequestHeader('Accept', this.options.headers.Accept);
response.execute();
var responseBody = response.getBody();
var jsonObject = JSON.parse(responseBody);
return jsonObject;
},

type: 'RestAPI'
};

 

And here's an example of what the widget's client script might look like:

 

function onLoad() {
// Call Script Include to retrieve data
var ga = new GlideAjax('<Script Include Name>');
ga.addParam('sysparm_name', 'getData');
ga.getXML(onSuccess);
}

function onSuccess(response) {
// Parse JSON object and dynamically display data
var responseBody = response.responseXML.documentElement.getAttribute('answer');
var jsonObject = JSON.parse(responseBody);
for (var i = 0; i < jsonObject.length; i++) {
// Display data in widget
// ...
}
}

 

In this example, <REST API URL> would be replaced with the URL of the REST API endpoint on the other ServiceNow instance that you want to retrieve data from, and <Script Include Name> would be replaced with the name of the Script Include that you created to make the REST API call.

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

1 REPLY 1

Syedmd08
Kilo Guru

To dynamically display data sent through REST API from one ServiceNow instance to another without storing it in any table, you can use the following approach:

  1. Create a ServiceNow Portal widget that will be used to display the data received from the REST API.

  2. Use a ServiceNow Script Include to make the REST API call to the other instance and retrieve the data. The Script Include can then return the data as a JSON object.

  3. In the widget's client script, use the GlideAjax class to call the Script Include and retrieve the data. You can then use JavaScript to parse the JSON object and dynamically display the data in the widget.

Here's an example of what the Script Include might look like:

 

var RestAPI = Class.create();

RestAPI.prototype = {
initialize: function() {
// Set up REST API options
this.options = {
url: '<REST API URL>',
method: 'GET',
headers: {
'Accept': 'application/json'
}
};
},

getData: function() {
// Make REST API call and return JSON object
var response = new sn_ws.RESTMessageV2();
response.setEndpoint(this.options.url);
response.setHttpMethod(this.options.method);
response.setRequestHeader('Accept', this.options.headers.Accept);
response.execute();
var responseBody = response.getBody();
var jsonObject = JSON.parse(responseBody);
return jsonObject;
},

type: 'RestAPI'
};

 

And here's an example of what the widget's client script might look like:

 

function onLoad() {
// Call Script Include to retrieve data
var ga = new GlideAjax('<Script Include Name>');
ga.addParam('sysparm_name', 'getData');
ga.getXML(onSuccess);
}

function onSuccess(response) {
// Parse JSON object and dynamically display data
var responseBody = response.responseXML.documentElement.getAttribute('answer');
var jsonObject = JSON.parse(responseBody);
for (var i = 0; i < jsonObject.length; i++) {
// Display data in widget
// ...
}
}

 

In this example, <REST API URL> would be replaced with the URL of the REST API endpoint on the other ServiceNow instance that you want to retrieve data from, and <Script Include Name> would be replaced with the name of the Script Include that you created to make the REST API call.

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!