Skip to main content
Version: 1.21.3

SimpleAjax

This class provides methods that allow the client-side script to be executed on the server side using Script Include.

To use the SimpleAjax class, complete the following steps:

  1. Call the SimpleAjax constructor to create a SimpleAjax instance. An argument for the constructor should be the name of the script include that involves the class with the necessary methods.
  2. Call the addParam(paramName, paramValue) method. Pass the required parameters defining the method needed as the main call. To do so, complete the following steps:
    1. set the sysparm_name parameter.
    2. and the method name taken from the defined class called by the script include.

To provide other parameters to the script include, call this method as many times as you need.

  1. Call the getXML(callback) method to execute the method declared above.
caution

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

To make a script include callable from the client-side scripts, activate the Client callable attribute.

See the Script Include article to learn more.

addParam(paramName, paramValue)


Use this method to pass 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.

caution

The server-side script only runs 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 the value to the function parameter.

Parameter(s):

NameTypeMandatoryDefault value
paramNameStringYN
paramValueStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

addParam()
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 related to the current SimpleAjax object is executed with the specified parameters. The request is processed asynchronously. The results of the server-side method execution will be passed to the specified callback function.

Return:

TypeDescription
VoidThis method does not return a value.

Example:

getXML()
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 runs with the ACL restrictions.

note

The callback function receives the answer element of the response.

Parameter(s):

NameTypeMandatoryDefault value
scriptValueStringYN
tableName
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:

runScript()
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. To do so, an admin role is necessary. There are no ACL restrictions for the script. If a user that does not have the admin role attempts to a run the script, the method returns an empty result as callback. Before using the script result, verify that the response.data.result is not empty.

note

The callback function receives the answer element of the response.

Parameter(s):

NameTypeMandatoryDefault value
scriptValueStringYN
tableName
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:

runAdminScript()
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);
}
)