Execute JavaScript

This activity executes a JavaScript code using the specified input parameters and returns the result in the output.

Note: This activity supports only ECMAScript 5-compatible functions. For more information about these functions, see https://www.w3schools.com/js/js_es5.asp.

You may see some code suggestions in the editor that are not ECMAScript 5 compatible. We recommend that you do not implement the suggestions. If you implement them, the activity may return some errors.

You can use this activity to easily write complex logic in JavaScript which may not be straightforward to achieve using the Flogo Enterprise mapper. For example, you can easily filter arrays based on some conditions or looping through arrays using forEach and other useful JavaScript functions.

Settings

Field Description
Javascript Code

Specify the JavaScript code to be executed in the following format:

var result, parameters
<JavaScript code>

Where:

  • The result variable must be defined for the output of the JavaScript.
  • The parameters variable must be defined for the input of the JavaScript.
    Set the input parameters on the Input Settings tab. Set the output parameters on the Output Settings tab.

Input Settings

Field Description
Script Input Parameters

Configure a schema for one or more input parameters to the JavaScript. The elements of this schema are available for mapping on the Input tab.

Use app-level schema: Click this button and select a schema that you might have defined earlier.

Change: To change the schema, click Change.

Syntax to access the input parameter value defined in the Javascript code:

parameters.<parameter_name>

For example, if you have defined the input parameter as foo in the JavaScript code, use the following syntax to access the value:

parameters.foo

Input

The Input tab displays the schema that you entered in the Input Settings tab in a tree format. Map the elements in the schema using the mapper or manually enter the value for the element in the mapper.

Output Settings

Field Description
Script Output

Configure a schema for one or more output parameters of the JavaScript.

Use app-level schema: Click this button and select a schema that you might have defined earlier.

Change: To change the schema, click Change.

Syntax for setting the value of the output parameter in the Javascript code:

result.<parameter_name>

Example: 

result.foo = bar

Output

The Output tab displays the output parameters from the schema you entered in the Output Settings tab. The output parameters are displayed in the result object in a tree format.

Field Description
error Flag indicating whether an error occurred while executing the JavaScript.
errorMessage The error message.
result The output of the JavaScript code, indicating a successful execution of the JavaScript.