Script Includes are used to store JavaScript functions and JavaScript functions and classes that will execute are executed on the server. Script Includes Include scripts generally specify either an object class or a function.
Creating Create a script include
A script include has a name, a description, and a script itself. Also, it is possible to specify options active or client-callable.
To create a new script include, please complete the following steps:
- Navigate to SystemDefinition → Settings → ScriptIncludes.
- Click New and fill in the fields.
- Click Save or Save and Exitexit to apply the changes.
Script Include form fields
Field | Mandatory | Description |
---|
Name | Y |
Script Specify the script include name. |
Should with declared used in the script field. Also, the script include name should be unique and can not contain spaces. |
Description | N |
Short action descriptionAdd a short description of the action. |
Active | N | Select this checkbox to activate this script. When selected, the script |
will execute is executed when it is called; otherwise, it |
will execute Callablecallable | N | Select this checkbox to make this script available to call on the client-side scripts. |
Disable ACL |
Checkcheck | N | Select this checkbox |
to let this so that the script include |
do does not go through an ACL check when it is executed within the Client Scripts. |
Script | Y | Define the server-side script to run when called from other scripts. Note |
---|
When invoking script includes in other server-side scripts, |
|
please use the ss.importIncludeScript method of the SimpleSystem class |
|
like on the example below: Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
| ss.importIncludeScript('EmailHelper');
const helper = new EmailHelper(); |
Otherwise, the script |
|
would be unavailablePractical case
Use cases
Use You can use client callable script includes in case if you need scripts to use the server-side methods on the client-side. For more clarity, let us consider Consider two simple examples of calling a script include from :a client-side
a and server-side.
Using
A practical use case is a calculation of the time period between two specified values using a specific schedule.
For this, let us create To do so, create a script include with the following parameters like shown below:
Name | DurationCalculator |
---|
Active | true |
---|
Client Callablecallable | true |
---|
Script | Enter a script from the code example belowthe following script: Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Client callable script include |
---|
linenumbers | true |
---|
| ss.importIncludeScript('AbstractAjaxProcessor');
var DurationCalculator = Class.create();
DurationCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDuration() {
const start = new SimpleDateTime(this.getParameter('sysparm_start'));
const end = new SimpleDateTime(this.getParameter('sysparm_end'));
const scheduleID = this.getParameter('sysparm_schedule');
const schedule = new SimpleSchedule(scheduleID);
// value that would be passed to client-side
return schedule.duration(start, end).getDurationSeconds() * 1000;
}
}); |
Note |
---|
When you call values passing from the client-side, use this.getParameter like like shown in a the code example above to get the sysparm_ parameter value. |
|
---|
On the client-side, we use SimpleAjax:
- Create a SimpleAjax instance.
- Transfer the called method name Pass the name of the method to be called using the reserved parameter parameter sysparm_name.
- Transfer Pass the values from a form using the the sysparm_start, sysparm_end, sysparm_schedule parameters.
- Call an the script include script using the getXML(callback) method.
- Process its response in the callback-function listed below.
- The answer variable will contain contains the value return by the getDuration() method.
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Using script include on the client-side |
---|
linenumbers | true |
---|
|
if (s_form.getValue('start') && s_form.getValue('end') && s_form.getValue('schedule_id')) {
const calculate = new SimpleAjax('DurationCalculator'); // call script include
calculate.addParam('sysparm_name', 'getDuration'); // call method
calculate.addParam('sysparm_start', s_form.getValue('start')); // pass param
calculate.addParam('sysparm_end', s_form.getValue('end')); // pass param
calculate.addParam('sysparm_schedule', s_form.getValue('schedule_id')); // pass param
calculate.getXML(callback);
function callback(response) {
// value received from server-side
const answer = response.responseXML.documentElement.getAttribute('answer');
s_form.setValue('duration', answer);
}
} |
Using Use a client callable script
include include on the lists
Another example is the calculation of list items after filtering.
For this, let us To do so, create script include with parameters like shown below:
Name | getListItemsCount |
---|
Active | true |
---|
Client Callablecallable | true |
---|
Script | Enter a script from the code example below: Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Client callable script include |
---|
linenumbers | true |
---|
| ss.importIncludeScript('AbstractAjaxProcessor');
var getListItemsCount = Class.create();
getListItemsCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCount() {
const table_name = this.getParameter('sysparm_table_name');
const condition = this.getParameter('sysparm_condition_query');
const list = new SimpleRecord(table_name);
list.addEncodedQuery(condition);
list.selectAttributes('sys_id');
list.query();
// value that would be passed to client-side
return list.getRowCount();
}
}); |
Note |
---|
When you call values passing from the client-side, use this.getParameter like like shown in a the code example above to get the sysparm_ parameter value. |
|
---|
The client-side code will be script is the following:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Using script include on the client-side |
---|
linenumbers | true |
---|
|
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const sAjax = new SimpleAjax('getListItemsCount'); // initialize script include
sAjax.addParam('sysparm_name', 'getCount'); // call method
sAjax.addParam('sysparm_table_name', s_list.getTablesName()[0]); // pass param
sAjax.addParam('sysparm_condition_query', urlParams.get('condition') || '()'); // pass param
sAjax.getXML(callback);
function callback(response) {
// value received from server-side
const answer = response.responseXML.documentElement.getAttribute('answer');
s_list.clearMessages();
s_list.addInfoMessage(`List items count: ${answer}`, 2000);
} |