ServicePortal Help?

ServiceNowEmp
Giga Expert

How does data flow between the server script and the client controller in a widget? What’s the best way to handle asynchronous updates?

3 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage

Hi @ServiceNowEmp 

 

 

When the widget loads, the server script executes first on the ServiceNow server. The data object returned from the server script is sent to the client controller (AngularJS). This data object becomes available as c.data inside the client controller.

If you need to refresh or update data after the initial load (without reloading the whole widget), use spUtil.get or $http.post to call the server script again asynchronously.

 

View solution in original post

Let me know if you need any Sample Code 

View solution in original post

// Server Script
(function() {
data.user = gs.getUserDisplayName();
data.incidents = [];
var gr = new GlideRecord('incident');
gr.addQuery('caller_id', gs.getUserID());
gr.query();
while (gr.next()) {
data.incidents.push({
number: gr.number.toString(),
short_description: gr.short_description.toString()
});
}
})();
js
Copy code
// Client Controller
function($scope) {
var c = this;
console.log(c.data.user); // Accesses value from server
}

View solution in original post

5 REPLIES 5

ServiceNowEmp
Giga Expert

Thanks @Ravi Gaurav  you are a Saviour