Service Portal Widget-working with server script

matthew_magee1
Giga Guru

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:

find_real_file.png

 

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!

find_real_file.png

6 REPLIES 6

Deepak Ingale1
Mega Sage
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" />

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.

Sweet! i'll give this a try today and report back

tomashrobarik
Giga Guru

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.