How to call a client-callable script-include function (accepting parameters) from server-side as well?

Pranshu3
Tera Expert

Hi all,

I have a client-callable script include in my instance. A function within this script accepts client parameters [this.getParameter('sysparm_sys_id')].

Now to reduce code redundancy, I would like to call the same function from server-side (business rule).

How to make this client-callable script-include function accept parameters and get utilized from server-side as well?

1 ACCEPTED SOLUTION

Pranshu3
Tera Expert

Hi, found the solution!

There is a one-line script to make a client-callable script-include function server-side accepted.

Add server type parameters to the function of script include:

    getIncidents: function(problemId){

Now within the function use the below code line:

    var probSysId = JSUtil.nil(problemId) ? this.getParameter('sysparm_sys_id') : problemId;

This way the function can accept parameters and can be called from both client & server side.

Thanks!

View solution in original post

7 REPLIES 7

Here is a whole solution:

 

//You call the client callable Script Include the same way as the server-side Script include

var scriptIncludeShortName = new ScriptIncludName();

 

//Then you call the Script Include function by dotwolking to his name

var valueReturnedBySIFunction = scriptIncludeShortName.theFunctionName(parameterOne, parameterTwo);

 

//Then you need to change the function declaration in the Script Include in a way that it can access values also from the server-side call

//That means:

//You need to change it from this:

functionName: function() {

     var firstParameter = this.getParameter("sysparm_parameter_one");

     var secondParameter = this.getParameter("sysparm_parameter_two");

     ...

}

 

// To this:

functionName: function(parameterOne, parameterTwo) {

     var firstParameter = parameterOne || this.getParameter("sysparm_parameter_one");

     var secondParameter = parameterTwo || this.getParameter("sysparm_parameter_two");

     ...

}

I hope that this will be helpful to someone.

Sajilal
Mega Sage

For Scoped Applications, use the below

var abc = new global.ScriptIncludename();
var result = abc.ScriptIncludeFunctionName(current.yourparameter);

Please mark the answer as Correct and Helpful, if it works for you.

Regards,
Sajilal

LearnUseThrive
Mega Sage

I believe @Earl Duque had a detailed blog post on this that he recently moved to Community somewhere. Look for that.

Edit: Here it is https://www.servicenow.com/community/developer-advocate-blog/client-callable-and-server-callable-scr...