Calling a function in the ScriptLibrary

ScriptLibrary records contain functions that can be called from anywhere you can call JavaScript. Using this technique allows for more effective management of user JavaScript functions. Functions can use parameters as part of the calling structure, and they can also return values.

First, you must establish a record in the ScriptLibrary that contains the function to be called. Within the record you must know the name of the script as well as create a properly-constructed function.

Note A record can contain more than one function.

This is an example of using more than one function.

/* Sample script system.library.functionTest */

function square(value)
{
	var ret_val = value * value;
	return ret_val;
}
function cube(value)
{
	var ret_val = value * value * value;
	return ret_val;
}

To call the function use the following syntax:

system.library.<script name>.<function name>(<Comma separated list of parameters>)

The following is an example of calling the functions of system.library.functionTest:

var x = 3;
var y = system.library.functionTest.square(x);
print(x, "  squared =  ", y);

x = -2;
y = system.library.functionTest.square(x);
print(x, "  squared =  ", y);

x = 2;
y = system.library.functionTest.cube(x);
print(x, "  cubed   =  ", y);

/*
The function calls produce the following output:
 3  squared =  9
-2  squared =  4
 2  cubed   =  8
*/

When working with the ScriptLibrary, you can compile the JavaScript code to check for syntax errors. Executing the script will compile the code and then run it. Before executing the code, ensure that all inputs are available.

You can access any script or function in the ScriptLibrary from any tool within Service Manager that supports JavaScript, by using the following syntax:

system.library.<scriptname>.<functionname>( parameters )

Within a ScriptLibrary record, you can call any function that is defined previously within the same record by simply typing:

<functionname>( parameters )

To call JavaScript from any RAD process panel, you can use the jscall RAD function. The syntax for this function is:

jscall( "script.function" , arg1 , arg2, ... , argN )

The following statements illustrate a JavaScript call to the system library and a call to the script library by using jscall(), respectively:

system.library.tzfunctions.getTZforOperator(“falcon”);
jscall("tzfunctions.getTZforOperator", “falcon”);

To call a JavaScript function from the operating system's command prompt, you can use the us.js.call RAD routine. The syntax for the command is:

sm us.js.call script.function arg1 arg2

As an example, the following command calls the UpdateAllInteractions.updateInteractionFields function, which needs the {"abc", "def"} arguments:

sm us.js.call UpdateAllInteractions.updateInteractionFields {\\\"abc\\\",\\\"def\\\"} NULL