Versions Compared

Key

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

This class allows the client-side script execution of the code on the server side using Script Includes.

To use the SimpleAjax class, complete the following steps:

  1. Call the SimpleAjax constructor to create a SimpleAjax instance. The name of the Script Include, which involves the class with the necessary methods, should be the argument for the constructor.
  2. Call the addParam(paramName, paramValue) method.

    When calling it

    Pass the required parameters defining the method needed as the main call. To do so, complete the following steps:

    1. provideset the sysparm_name as a parameter
    2. and the method name taken from the include part of the script calleddefined class called by Script Include.

    To provide other parameters to the script code Script Include, call this method as many times as you neednecessary.

  3. Сall the Call the getXML(callback) method to execute the method declared above.
Note

The class name declared in the Script Include record should match the Script Include name.

To make the Script Include callable from client-side scripts, activate the Client Callable attribute.

For more information and practical cases, refer to the Script Includes article.

addParam(paramName, paramValue)


This method passes the specified parameter name and value to the server-side function associated with the current SimpleAjax object. The addParam() method can be used several times with different parameters and values.

Note

The server-side code runs only after getXML() is called by the client script.

The parameter name should start with the sysparm_ prefix. Avoid using a predefined sysparm_name parameter name for passing value parametersfor passing the value to the function parameter.


Parameter(s):

NameTypeMandatoryDefault value
paramNameStringYN
paramValueStringYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titleaddParam
linenumberstrue
const sAjax = new SimpleAjax('DurationCalculator'); // call script include
sAjax.addParam('sysparm_name', 'getDuration'); // call class method
sAjax.addParam('sysparm_start', s_form.getValue('start')); // pass parameter
sAjax.getXML(callback);

function callback(response) {
  const answer = response.responseXML.documentElement.getAttribute('answer');
  console.log(answer);
}

getXML(callback)


By calling this method, the client script sends a request to the server where the method and parameters related parameters related to the current SimpleAjax object are executed. The request is processed asynchronouslyprocessed asynchronously.  The results The results of the serv]er-side method execution will be returned via passed to the function specified as a callback function


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlegetXML
linenumberstrue
const sAjax = new SimpleAjax('DurationCalculator'); // call script include
sAjax.addParam('sysparm_name', 'getDuration'); // call class method
sAjax.addParam('sysparm_start', s_form.getValue('start')); // pass parameter
sAjax.getXML(callback);

function callback(response) {
  const answer = response.responseXML.documentElement.getAttribute('answer');
  console.log(answer);
}


runScript(scriptValue, tableName, callback)


Use this method to run a script against a table specified. The script is run runs with the ACL restrictions.

Info

The callback function receives the answer element of the response.


Parameter(s)Parameters:

NameTypeMandatoryDefault Value
scriptValueStringYN

tableName

Tooltip
onlyIcontrue
appendIconinfo-filled
iconColorblue

This parameter is used for displaying information and the script run result on some pages, such as record/sys_script.


StringNN
callbackFunctionNN

Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlerunScript
linenumberstrue
const CURRENT_ID = s_form.getUniqueValue();

const simpleAjax = new SimpleAjax();
await simpleAjax.runScript(
  `const current = new SimpleRecord('task');
  current.get('${CURRENT_ID}');
  setResult(current.caller.email)`,
  null,
  (response) => {
    console.log(response.data.result);
  }
)


runAdminScript(scriptValue, tableName, callback)


Use this method to run a script by a user with the . To do so, an administrator role is necessary. There are no ACL restrictions for the script. If a user, who does not have the administrator role, attempts to a run the script, the method returns an empty result as callback. Therefore, before using the script result, verify that the response.data.result is not empty.

Info

The callback function receives the answer element of the response.


Parameter(s)Parameters:

NameTypeMandatoryDefault Value
scriptValueStringYN

tableName

Tooltip
onlyIcontrue
appendIconinfo-filled
iconColorblue

This parameter is used for displaying information and the script run result on some pages, such as record/sys_script.


StringNN
callbackFunctionNN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlerunScript
linenumberstrue
const simpleAjax = new SimpleAjax();
await simpleAjax.runAdminScript(
  `const supportSiteURL = ss.getProperty('simple.auth_page.support_site');
  setResult(supportSiteURL)`,
  null,
  (response) => {
    const serverData = response.data.result;
    console.log(serverData);
  }
)


Table of Contents
absoluteUrltrue
classfixedPosition