Run JavaScript

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

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

This activity supports both ECMAScript 5-compatible and ECMAScript 6-compatible functions. For more information, see ECMAScript 5-compatible functions or ECMAScript 6-compatible functions.

Important Considerations

  • You might see some code suggestions in the editor that are not valid for ECMAScript 5-compatible or ECMAScript 6-compatible functions. Do not implement these suggestions, as they might cause the activity to return errors.
  • For ECMAScript 6-compatible functions, the following are not supported.
    • Classes
    • Promises
    • Default parameters
    • Function REST parameter
    • Arrow functions

Settings

Field Description
Javascript Code

Specify the JavaScript code to be run 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 Use app-level schema 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 Use app-level schema 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 that 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 running the JavaScript.
errorMessage The error message.
result The output of the JavaScript code, indicates successful execution of the JavaScript.