Basic Practices for Custom Scripts Developers

Most Used Custom Script Commands

Any Custom Script is represented by the customScript object that encapsulates a set of methods and properties. These methods and properties can be utilized to simplify the creation and usage of Custom Scripts. This section considers the most used of them in a more detail.

Methods

customScript.runApiRequest()

The runApiRequest() method utilizes jQuery's ajax() method.

It accepts two parameters:

  • url:  the URL to which the request is sent
  • settings: a set of key/value pairs that configure the Ajax request; if no settings are passed, the default settings are used

Example: Obtain a list of all Datasets and their metadata by executing customScript.runApiRequest(customScript.homeSite+'api/dataset');

customScript.close()

Finishes the script's execution.

Example:  setTimeout(function(){ customScript.close(); },1000);

customScript.log()

This method is an analogue of the console.log() method. The output is displayed in the Script Output section and on the Run History tab.

Example: customScript.log("Logging the custom script's message");

customScript.error()

Outputs an error that is displayed in the Script Output section and on the Run History tab.

Example: customScript.error('Dataset with name '+customScript.parameters.datasetName+' is not found');

Properties

customScript.parameters

customScript.parameters.<parameter name> is used to access Custom Script's parameters, that were specified in the Custom Script Editor.

Example: customScript.log(customScript.parameters.datasetName); logs the script's datasetName parameter value

customScript.homeSite

The customScript.homeSite property holds the value of the MI instance's URL

Example: customScript.log(customScript.homeSite); logs the URL of the MI instance

Debugging Custom Scripts

To execute your Custom Script in the debug mode, simply replace edit in the script's URL with debug: <hostname>/admin/customscript/edit/id/<script ID> 🠮 <hostname>/admin/customscript/debug/id/<script ID>