Help on using script include on Service Portal widget

JC S_
Mega Guru

I am developing a custom widget on service portal and I have all the logic coded out on the Server Script. Now I realized that i can reuse some of the logic and want it to be reusable in the future for other widgets.

How can I set up a script include then pass a variable from server script to that script include and have the script include process that variable and return a value?

 

SAMPLE:

//OLD SERVER SCRIPT CODE:

var x = '0';

//The code below is a sample of what I want to be reusable in other widgets through script include
if (x == '0'){
// do nothing
} else {
//do this one
x = x + 1;
}
data.msg = x;

 

//NEW SERVER SCRIPT CODE:
var x = '0';

data.msg = new myScript().test(x);

I tried above and the variable x seems to be not being processed by script include.

 

SCRIPT INCLUDE: (updated the test: function(x) part as suggested)

var myScript = Class.create();
myScript.prototype = {
initialize: function() {
},
	
test: function(x) {
	
	if(x==0){   
		return true;
	} else {
		return false;
	}
},
	
	isPublic: function() {
		return true;
	},

type: 'myScript'
};
1 ACCEPTED SOLUTION

can you update the test function like below and check :

test: function (x)

{

if(x==0)

{

return true;

}

else {

return false;

}

},

View solution in original post

18 REPLIES 18

Syvo
Mega Guru

Hi,

You can use a script include in a widget server script just like would in a business rule:

var myScriptInclude = new MyScriptInclude();
var test = myScriptInclude.test(x);

HTH
Sylvain

The widget is on a public page, as such I added the isPublic part on the script include, still does not work though.

JC,

You can add a gs.log in your script include in the initialize function to make sure the object is initialized.

Like this? There's an error when I added that.
find_real_file.png

Nope,

gs.log("My object is being initialized!", "myScriptSource");

Then open system logs and filter by source "myScriptSource".