Get server data in widget client controller
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.
https://www.servicenow.com/community/developer-articles/get-server-data-in-widget-client-controller/ta-p/2330106