SatheeshKumar
Kilo Sage

Problem: GlideAjax is not supported in widget client controller. so it can't be used to get data from server KB0691908

 

Alternate Methods to get server data to client controller of widget!!

Method 1:
Using server.get() method to get data from server.

HTML

<button type="button" ng-click="c.getIncidents()" class="btn btn-success">Get Incident List</button>

//A button to trigger the server call

Client Controller:

c.getIncidents = function() {
c.server.get({
parmeter1: "dummy",
action: "getIncident"
}).then(function(response) {
//Process your response
c.data.retrivedList = response.data.incList;
alert("Received data from server" + c.data.schedList.toString());
});
}

we can pass required parameters to server in the below format

{parmeter1: "dummy",action: "getIncident"}

Any number customer properties can be added to object and can be passed to server.

In the above example action property is used to determine what action is to be performed in server side(to find the correct block of code)., Also it is a custom property, it can be modified as required.


function(response)//callback function

Server process the input and set the results to data object.After server execution the data object is copied to response object.

Server:

 

if (input && input.action == "getIncident") {
gs.addInfoMessage("Received Parameter: "+ input.parmameter1)
var incList = [];

var inc = new GlideRecord("incident");
inc.setLimit(5);
inc.query();
while(inc.next()) {

incList.push(inc.number + '');

}
data.incList = incList;

}

Object passed to server.get() is copied to the input object in server, So you can access the passed properties using the input object.

 

i.e

input.action will contain "getIncident"
input.parameter1 will contain "dummy"

 

Difference between server.get() and server.update()


this.server.get([Object]) --> Calls the server and sends custom input. Returns Promise.
this.server.update() --> Calls the server and this.data is automatically send to server side. Returns Promise.

On calling server.update(), the client script data object is automatically overwritten by the server script data object

On calling server.get(), only the custom object is automatically overwritten by the server script input object

===================================================================================================

Method 2: Using a http client to make a rest call (consuming the API's of servicenow) to get the data.

HTML:
<button type="button" ng-click="c.getInc()" class="btn btn-success">Get Incident</button>


Client script :

c.getInc = function() {
$http.get('https://dev51484.service-now.com/api/now/table/incident/cc7d9d04db8d8410b6db8e4748961944').then(function(response){
alert(JSON.stringify(response));

});

Note: Don't forget to pass $http in function.

In this method server call made using http client as a rest call and data is retrieved as response of rest call


Limitation:

You can't manipulate complex data changes/filtering using this method, if needed you should required filtering/manipulation in API itself.

Preview:find_real_file.pngA working example is added in attachment( widget XML)

Name of widget : serverCall 

 

 -Satheesh

Comments
Poonam Mahamuni
Tera Contributor

I am working on the same code and passing a string from server to client. It returns [object] [object] in client controller response block. Any help is appreciated..

AnirudhKumar
Mega Sage

I don't think it's string anymore... along the way it must have become a JSON.

Do this. let's say your variable is x, then in client controller

alert(JSON.stringify(x));

Version history
Last update:
‎11-06-2019 09:37 AM
Updated by: