Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Include scripts are used to store JavaScript functions and classes that are executed on the server. Include scripts generally specify either an object class or a function.
Tip |
---|
Role required: admin. |
Create an include script
An include script 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, complete the following steps:
- Navigate to SystemDefinition → ScriptIncludes.
- Click New and fill in the fields.
- Click Save or Save and Exit to apply the changes.
Script Include form fields
Field | Mandatory | Description | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Name | Y | Specify the include script name. It should match with the class name used in the script field. Also, the include script name should be unique and can not contain spaces. | |||||||||||
Description | N | Add a short description of the action. | |||||||||||
Active | N | Select this checkbox to activate this script. When selected, the script is executed when it is called; otherwise, it is not executed. | |||||||||||
Client Callable | N | Select this checkbox to make this script available to call on the client-side scripts. | |||||||||||
Disable ACL Check | N | Select this checkbox so that the include script 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.
|
Use cases
Use client callable include scripts in case if you need scripts to use the server-side methods on the client-side. Consider two simple examples of calling an include script from :a client-side
aand server-side.
Use a client callable include script on the forms
A use case is a calculation of the time period between two specified values using a specific schedule.
To do this, create an include script with the following parameters:
Name | DurationCalculator | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Active | true | |||||||||||||
Client Callable | true | |||||||||||||
Script | Enter the following script:
|
On the client-side, use SimpleAjax:
- Create a SimpleAjax instance.
- Pass the name of the method to be called using the reserved parameter sysparm_name.
- Pass the values from a form using the sysparm_start, sysparm_end, sysparm_schedule parameters.
- Call the include script using the getXML(callback) method.
- Process its response in the callback-function listed below.
- The answer variable contains the value return by the getDuration() method.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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); } } |
Use a client callable include script on the lists
Another example is the calculation of list items after filtering.
To do this, create script include with parameters like shown below:
Name | getListItemsCount | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Active | true | |||||||||||||
Client Callable | true | |||||||||||||
Script | Enter a script from the code example:
|
The client-side script is the following:
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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); } |
Table of Contents | ||||
---|---|---|---|---|
|
...