JavaScript

Description

The JavaScript action runs Javascript code and returns a boolean expression.

The result can be used to determine which action will be executed next.

You can use functions, procedure calls, ANDs, ampersands, ORs, EQUALs, etc.

The Javascript workflow action evaluates and returns a true or false.

Options

OptionDescription

Workflow action name

The name of the workflow action. Note: This name has to be unique in a single workflow. A workflow action can be placed several times on the canvas, however it will be the same workflow action.

JavaScript

The JavaScript field.

Evaluation

The result of a JavaScript action is either true or false. In other words, it needs to end with a boolean expression.

Here are a few possible evaluations to end your script with:

  1. lines_input > 100

or

  1. true

or

  1. parent_workflow.getVariable("INPUT_DIRECTORY").equals("/tmp");

The following variables are available for the expression:

VariableDescription

errors

Number of errors in the previous workflow action (long).

lines_input

Number of rows read from database or file (long).

lines_output

Number of rows written to database or file (long).

lines_updated

Number of rows updated in a database table (long).

lines_read

number of rows read from a previous pipeline transform (long).

lines_written

Number of rows written to a next pipeline transform (long).

files_retrieved

Number of files retrieved from an FTP server (long).

exit_status

The exit status of a shell script (integer).

nr

The workflow action number of the previous workflow action (long); increments at every next workflow action.

is_windows

use if Hop runs on Windows (boolean).

parent_workflow

The parent workflow of the current workflow action.

action

The current workflow action.

Variables

Here is how you can evaluate the content of a variable string:

  1. parent_workflow.getVariable("NR_OF_ROWS") == 1000000;

Since we have access to the parent_workflow object, we can also set variables in the parent workflow this way:

  1. parent_workflow.setVariable("NR_OF_ROWS", "1000000");

For example you can do something like the following to manipulate variables within this workflow action:

  1. useDate = parent_workflow.getVariable("use_date").equals("1");
  2. if (useDate == 0) {
  3. date = new java.util.Date();
  4. date.setDate(date.getDate()-1); //Go back 1 full day
  5. dateFormat = new java.text.SimpleDateFormat("yyyyMMdd");
  6. newDateStr = dateFormat.format(date);
  7. parent_workflow.setVariable("start_date", newDateStr);
  8. }
  9. true;

Previous result

When a workflow action finishes, the result of the execution will be a Result object exposed as “previous_result” to the JavaScript engine:

ExpressionAlternativeData typeMeaning

previous_result.getResult()

boolean

true if the previous workflow action was executed successfully, false if there was some error.

previous_result.getExitStatus()

exit_status

int

exit status of previous shell script workflow action

previous_result.getActionNr()

nr

int

The action number is increased every time a workflow action is executed.

previous_result.getNrErrors()

errors

long

the number of errors, also available as variable “errors”.

previous_result.getNrLinesInput()

lines_input

long

The number of rows read from a file or database.

previous_result.getNrLinesOutput()

lines_output

long

The number of rows written to a file or database.

previous_result.getNrLinesRead()

lines_read

long

The number of rows read from previous transforms.

previous_result.getNrLinesUpdated()

lines_updated

long

The number of rows updated in a file or database.

previous_result.getNrLinesWritten()

lines_written

long

The number of rows written to next transform.

previous_result.getNrLinesDeleted()

lines_deleted

long

The number of deleted rows.

previous_result.getNrLinesRejected()

lines_rejected

long

The number of rows rejected and passed to another transform via error handling.

previous_result.getRows()

List<RowMetaAndData>

The result rows, see also below.

previous_result.isStopped()

boolean

Flag to signal if the previous previous workflow action stopped or not.

previous_result.getResultFilesList()

List<ResultFile>

The list of all the files used in the previous workflow action (or actions).

previous_result.getNrFilesRetrieved()

files_retrieved

int

The number of files retrieved from FTP, SFTP, etc.

previous_result.getLogText()

String

The log text of the execution of the previous workflow action and its children.

previous_result.getLogChannelId()

String

The ID of the log channel of the previous workflow action. You can use this to look up information on the execution lineage in the log channel log table.

Rows

The “rows” variable we expose to JavaScript helps you evaluate the result rows you passed to the next workflow action using the “Copy rows to result” transform. Here is an example script on how to use this array:

  1. var firstRow = rows[0];
  2. firstRow.getString("name", "").equals("Foo")
  1. This script will follow the green workflow hop is the expression evaluates to true. This happens if field "name" contains String "Foo".