Getting Started: Functions

What Does This Article Cover?

  • What is a Function?
  • Function considerations
  • Function example
  • Other related material

What is a Function?

Snippets of JavaScript code may be used in the Intelligence Hub to change values, check for specific values, or perform logic. Functions allow the code to be defined once and then reused anywhere in the Intelligence Hub that supports JavaScript. The syntax is basic JavaScript. Intelligence Hub uses GraalVM to execute the JavaScript.

Function Considerations:

When using Functions consider the following

  • To test a function, it is often necessary to write test code to call the function and log output results to the console.
  • When editing a function that is already in use in multiple locations, consider that changes are instantly available everywhere the function is referenced. So only save changes once you have confirmed the functions work as expected.
  • The Intelligence Hub supports importing npm-compatible CommonJS modules and using them in expressions. To use this feature, third-party modules must be installed within the expression-imports directory of the hub’s directories.appData directory. Use the npm install command from the expression-imports directory to install third-party modules for use by the Intelligence Hub. CommonJS packages that rely on built-in Node.js core Modules or native NPM modules are not supported.

Example Function:

It might be beneficial to consistently obtain a date and time with a specific format. This is an example of an opportunity to implement a simple function.

To access Functions, navigate to Functions in the configuration Main Menu. Enter the JavaScript in the Code section. Functions can be tested by selecting Run. This executes the JavaScript in the browser and outputs the results to the right of the Code panel.

The following is the JavaScript code for the function illustrated above.

// ISO8601 formatted date
function getISODateString() {
        const currentDate = Date.now();
        const today = new Date(currentDate);
        today.toISOString();
        return today;

``

Other related material: