How to write client controller and server script ?

sneha64
Tera Contributor

Hello,

I'm a novice when it comes to building widgets.

Instead of using catalog client script and script include, I have a requirement that we write our logic in widget (using client controller and server script)

I've already written the logic, and it's working fine.
Can somebody assist me in building the client controller and server scripts for this?

 

Catalog Client script code:

var ga = new GlideAjax('saveDefaultDeliveryPreferences');
ga.addParam('sysparm_name','savingCountryAndDeliverAddress');
ga.addParam('sysparm_country', g_form.getValue('country'));
// ga.addParam('sysparm_delivery', g_form.getValue("delivery_address"));
ga.getXML(updatePrefernceDetails);
function updatePrefernceDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

 

Script include code:

var saveDefaultDeliveryPreferences = Class.create();
saveDefaultDeliveryPreferences.prototype = Object.extendsObject(AbstractAjaxProcessor, {
savingCountryAndDeliverAddress: function(){

var country;
var currentUser = gs.getUser();
var sysId = this.getParameter("sysparm_country");
if (sysId) {
var gr = new GlideRecord("cmn_location");
if (gr.get(sysId)) {
country = gr.getValue("country");
}
}
country = JSON.stringify(country);
currentUser.savePreference('defaultCountry',country);

},
type: 'saveDefaultDeliveryPreferences'
});

 

Regards,

Sneha

1 ACCEPTED SOLUTION

Couple of links are mentioned below which are quite good for learning Service POrtal widgets:

https://www.youtube.com/watch?v=Jav32aDEz1I

https://www.youtube.com/watch?v=0gz3XzBM-Gs

https://developer.servicenow.com/print_page.do?release=paris&category=courses&identifier=app_store_learnv2_serviceportal_paris_service_portal&module=course

 

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/build/service-portal/concept/widget-dev-guide.html

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/build/service-portal/concept/adv-widget-tutorial.html

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

8 REPLIES 8

shloke04
Kilo Patron

Hi,

Please use below to have the current logic in a Widget:

Fist you need to write a Server Side script as in Widget this is the component which gets loaded first:

Server Side:

if (input && input.action == "getCountry") {
 data.retrievedValue = new Your ScriptInclude Name().functionName(input.parameter1);
}



Just pass your Script include and Function Name in above snippet

Next comes your Client Controller which you need to modify as below:

In Client controller, define in first line $scope and also define in next line "var c = this;" as shown below:

Then use below Client Controller Script:

c.getCountryDetails = function(){
		c.server.get({
		parmeter1:$scope.page.g_form.getValue('country'),
action: "getCountry"

	}).then(function(response) {
//Process your response
	c.data.retrivedDate = response.data.retrievedValue;

 

Now in HTML you can pass the final output which you are getting from your script include passed from Server to Client controller in the variable "c.data.retrivedDate"

So your HTML will look something like below:

<div ng-click = "c.getCountryDetails()">Your Output</div>

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

sneha64
Tera Contributor

As per you:
if (input && input.action == "getCountry") {
data.retrievedValue = new Your ScriptInclude Name().functionName(input.parameter1);
}

if you see my Script include my function didn't have any parameter:

savingCountryAndDeliverAddress: function(){

var country;
var currentUser = gs.getUser();
var sysId = this.getParameter("sysparm_country");
if (sysId) {
var gr = new GlideRecord("cmn_location");
if (gr.get(sysId)) {
country = gr.getValue("country");
}
}
country = JSON.stringify(country);
currentUser.savePreference('defaultCountry',country);

},
type: 'saveDefaultDeliveryPreferences'
});

so how to call script include function in server side?

Hi,

Please check your Client script where you are passing a parameter by writing below line:

ga.addParam('sysparm_country', g_form.getValue('country'));

and getting that value in Script Include by using the syntax "getParameter"

So while using widget you need to follow the way I have mentioned above. Did you tried what I have suggested, if you are stuck do let me know.

 

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

sneha64
Tera Contributor

This is server side script:

(function() {

if (input && input.action == "getCountry") {
data.retrievedValue = new saveDefaultDeliveryPreferences().savingCountryAndDeliverAddress(input.parameter1);
}

})();

client controller:

function($scope, $rootScope, spSCNavStateManager, spUtil) {
//api.controller=function() {
/* widget controller */
var c = this;

c.getCountryDetails = function() {
c.server.get({
parmeter1: $scope.page.g_form.getValue('country'),
action: "getCountry"

}).then(function(response) {
//Process your response
c.data.retrivedDate = response.data.retrievedValue;
});
}
}

script include:

var saveDefaultDeliveryPreferences = Class.create();
saveDefaultDeliveryPreferences.prototype = Object.extendsObject(AbstractAjaxProcessor, {
savingCountryAndDeliverAddress: function(parameter1){

var country;
var currentUser = gs.getUser();
//var sysId = this.getParameter(parameter1);
if (parameter1) {

var gr = new GlideRecord("cmn_location");
if (gr.get(parameter1)) {
country = gr.getValue("country");
}
}
country = JSON.stringify(country);

currentUser.savePreference('defaultCountry',country);
},
type: 'saveDefaultDeliveryPreferences'
});

 

The variable i created in catalog item with type macro and widget assign to it:

 

 

find_real_file.png

 

and--

 

find_real_file.png

 

 

 

The requirement is contained within an order guide; in a CatLog item, there are two fields entitled Country and deliver address(which is contained within a variable set), and I need to capture their details based on user preference in order to avoid modifying the sys_user table.

 

Regards,

Sneha