Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Get API data asynchronously in portal widget.

M_Taimoor Jawai
Tera Expert

Hi,

I am creating a chart.js widget and it is getting its data from servicenow table using REST api. I want that when data changes in table and I save it. It should automatically be shown on widget without loading the page. Any suggestions?

CLIENT SCRIPT

api.controller=function($scope) {
/* widget controller */
var c = this;

$scope.labels = c.data.labels;


$scope.data = c.data.datas;
};

 

 

SERVER SCRIPT:

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

data.labels = [];
data.datas = [];
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://dev60543.service-now.com/api/now/table/u_story_2_dashboard?sysparm_fields=u_data&sysparm_lim...');
request.setHttpMethod('GET');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'Qmyl91I!K/Ib';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");

var response = request.execute();
var res =JSON.parse(response.getBody());
//var ar = JSON.parse(res)
//.u_data;
var result = res.result;
for(var i = 0; i<result.length;i++){

data.labels.push((JSON.parse(result[i].u_data).label).toString());
data.datas.push(JSON.parse(result[i].u_data).data);
}
})();

1 ACCEPTED SOLUTION

M_Taimoor Jawai
Tera Expert
2 REPLIES 2

M_Taimoor Jawai
Tera Expert

solved

 

Sails
Kilo Sage

Can you write up how did you achieve it?

Sails
ServiceNow Consultant
United Kingdom