Service Portal Widget-working with server script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2018 12:56 PM
Hi all,
I'm new to service portal widgets. I've created a simple widget, but am having trouble putting the pieces together. I've looked at some videos and online docs, but am still struggling. So here's my situation:
I want to grab the value selected from a variable on a form and display that in a widget. I've been able to do that w/ the code below using the HTML Template and Client Script. Now what I want to do is to perform some server side calculations and finish up the statement presented to the user regarding shipping. Here is the catalog form:
Here is my code:
HTML:
<div>
<p>{{ c.data.message }}</p>
</div>
Client Script:
function($scope) {
var c = this;
//Watch for changes in the u_network variable
$scope.$watch(function () {
return $scope.page.g_form.getValue('u_network');
}, function (value) {
//Update local data object with data from variable
if (value){
c.data.message = 'You have selected Network: ' + value +'. Your estimated delivery is: ' + **get value from server script**;
}
});
}
For the server calculation, I want to get the value of the variable (u_network), do a GlideRecord Lookup, and then spit an answer back out to the client to show the delivery time.
So the finished product would look something like this:
You have selected Network A. Your estimated delivery is 2-3 weeks.
The 2-3 weeks is what I want to pull back from the server script
Any help is greatly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2018 09:52 PM
Your sample client side controller code should be like
$scope.$watch(function () {
return $scope.page.g_form.getValue('ci');
}, function (value) {
c.data.message = value ? value : '';
c.data.action = "getInfo";
$scope.server.update().then(function(r){
alert("Response is " + JSON.stringify(r));
c.data.action = undefined;
});
});
// Server side code should be like
(function() {
if (input && input.action == "getInfo") {
var gr = new GlideRecord("cmdb_ci_computer");
gr.addQuery("sys_id", input.message);
gr.query();
if (gr.next()) {
data.name = gr.getValue("name");
data.serial_number = gr.getValue("serial_number");
data.os = gr.getValue("os");
data.asset_tag = gr.getValue("asset_tag");
}
}
})();
// And HTML should be like
<label>Name</label>
<input type="text" ng-model="c.data.name" />
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2018 09:53 PM
This is the working sample code which I have, you need to modify it per your requirement 🙂
Note: Please mark reply as correct / helpful if it answers your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-09-2018 04:26 AM
Sweet! i'll give this a try today and report back

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-09-2018 09:05 AM
Do you really need that special variable of type widget? If you just need to present some information to the user based on selected variable, why not just use simple onChange client script and using showFieldMsg function on g_form.