Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Tip

Role required: admin.

Creating 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:

  1. Navigate
to SystemDefinition ->
  1. to System Settings → ScriptIncludes.
  2. Click New and fill in the fields.
  3. Click Save or Save and Exit to apply changes.
 

Script Include form fields

FieldMandatoryDescription
NameYScript include name. Should  Should match with the class name declared in the script field. Also, script include name should be unique and can not contain spaces.
DescriptionNShort action description.
Active
When set to TRUE, this Select this checkbox to activate this script. When selected, the script will execute when called; otherwise, it will not execute.
Client Callable
When set to TRUE, this script will be 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.
ScriptY

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 shown on the example below:

Code Block
languagejs
themeEclipse
linenumberstrue
ss.importIncludeScript('EmailHelper');
const helper = new EmailHelper();

Otherwise, the script would be unavailable.


Practical case


You can call a server-side script include use client callable script includes in case you need to use server-side methods on the client-side. To perform this, activate the Client Callable attribute.

Script includes on client-side

For more clarity, let us consider a two simple example examples of calling a script include from:

  • a client-side
.
  • a server-side

Using client callable script include on forms


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:

NameDurationCalculator
Activetrue
ClientCallable
Client Callabletrue
Script

Enter a script from the code example below:

Code Block
languagejs
themeEclipse
title
Server-side
Client callable script include
on the client-side
linenumberstrue
ss.importIncludeScript('AbstractAjaxProcessor');

var DurationCalculator
var DurationCalculator = Class.create();
DurationCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getDuration
    getDuration() {
    const
        const start =
 new SimpleDateTime
 new SimpleDateTime(this.getParameter('sysparm_start'));
    const
        const end =
 new SimpleDateTime
 new SimpleDateTime(this.getParameter('sysparm_end'));
    const
        const scheduleID =
 this
 this.getParameter('sysparm_schedule');
    const
        const schedule =
 new SimpleSchedule
 new SimpleSchedule(scheduleID);
    return schedule.duration(start, end).getDurationSeconds() * 1000;
        // 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 shown in a code example above to get the sysparm_ parameter value.


In another example listed below, we use SimpleAjax on a On the client-side, and that's what we dowe use SimpleAjax:

  1. Create a SimpleAjax instance.
  2. Transfer the called method name using the reserved parameter sysparm_name.
  3. Transfer values from a form using the sysparm_start, sysparm_end, sysparm_schedule parameters.
  4. Call an include script using the getXML(callback) method.
    1. Process its response in the callback-function listed below.
  5. The answer variable will contain value return by the getDuration() method.
Code Block
languagejs
themeEclipse
titleScript Using script include on the client-side
linenumberstrue
if if (s_form.getValue('start') &&
  s s_form.getValue('end') &&
  s s_form.getValue('schedule_id')) {

  const    const calculate = new SimpleAjax new SimpleAjax('DurationCalculator');  // call script include
  calculate    calculate.addParam('sysparm_name',  'getDuration'); // call method
  calculate    calculate.addParam('sysparm_start', s_form.getValue('start')); // pass param
  calculate    calculate.addParam('sysparm_end', s_form.getValue('end')); // pass param
  calculate    calculate.addParam('sysparm_schedule', s_form.getValue('schedule_id')); // pass param
  calculate    calculate.getXML(callback);
  function callback

    function callback(response) {
    	// value received from server-side
        const answer = response.responseXML.documentElement.getAttribute('answer');
        s_form.setValue('duration', answer);
    }
}

Using client callable script include on lists


Another example is the calculation of list items after filtering.

For this, let us create script include with parameters like shown below:

NamegetListItemsCount
Activetrue
Client Callabletrue
Script

Enter a script from the code example below:

Code Block
languagejs
themeEclipse
titleClient callable script include
linenumberstrue
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 shown in a code example above to get the sysparm_ parameter value.


The client-side code will be the following:

Code Block
languagejs
themeEclipse
titleUsing script include on the client-side
linenumberstrue
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
    s_form.setValue('duration', answer);
  }    const answer = response.responseXML.documentElement.getAttribute('answer');
    s_list.clearMessages();
    s_list.addInfoMessage(`List items count: ${answer}`, 2000);
}


Table of Contents
absoluteUrltrue
classfixedPosition

...