You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 34 Next »

Script includes used to store JavaScript functions and classes that will execute on the server. Script includes generally specifying either object class or function.


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 if it is active or client-callable.  

To create a new script include, please complete the following steps:

  • Navigate to System Definition -> Script Includes.
  • Click New and fill in the fields.
  • Click Save or Save and Exit to apply changes. 


FieldDescription
NameScript include name. Should match with the class name declared in the script field. Also, script include name should not contain spaces.
DescriptionShort action description
ActiveWhen set to TRUE, this script will execute when called; otherwise, it will not execute.
Client callableWhen set to TRUE, this script will be available to call on the client-side scripts.
Script

Define the server-side script to run when called from other scripts

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:

ss.importIncludeScript('EmailHelper');
const helper = new EmailHelper();

Otherwise, the script would be unavailable.

Practical case


You can call a server-side script include in case if 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 simple example of calling a script include from a client-side.

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
ClientCallabletrue
Script

Enter a script from the code example below

Server-side script include on the client-side
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);
    return schedule.duration(start, end).getDurationSeconds() * 1000; // value that would be passed to client-side
  }
});

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 client-side, and that's what we do:

  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.


Script include on the client-side
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) {
    const answer = response.responseXML.documentElement.getAttribute('answer'); // value received from server-side
    s_form.setValue('duration', answer);
  }
}

  • No labels