Script Includes are used to store JavaScript functions and classes that will execute on the server. Script Includes generally specify either an object class or a function.
Role required: admin. |
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:
Script Include form fields
Field | Mandatory | Description | ||
---|---|---|---|---|
Name | Y | Script include name. Should match with the class name declared in the script field. Also, script include name should be unique and can not contain spaces. | ||
Description | N | Short action description. | ||
Active | Select this checkbox to activate this script. When selected, the script will execute when called; otherwise, it will not execute. | |||
Client Callable | Select this checkbox to make this script available to call on the client-side scripts. | |||
Disable ACL Check | Select this checkbox to let this script include do go through an ACL check when executed within Client Scripts. | |||
Script | Y | Define the server-side script to run when called from other scripts.
|
You can call a script include in case you need to use server-side methods on the client-side. To perform this, the Client Callable attribute. For more clarity, let us consider two simple examples of calling a script include from:
A practical case is a calculation of the time period between two specified values using a specific schedule.
For this, let us create script include with parameters like shown below:
Name | DurationCalculator | ||
---|---|---|---|
Active | true | ||
Client Callable | true | ||
Script | Enter a script from the code example below:
|
In another example listed below, we use SimpleAjax on a client-side, and that is what we do:
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); } } |
Another example is the calculation of list items after filtering.
For this, let us create script include with parameters like shown below:
Name | getListItemsCount | ||
---|---|---|---|
Active | true | ||
Client Callable | true | ||
Script | Enter a script from the code example below:
|
The client-side code will be the following:
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); } |