JavaScript
Description
Use the JavaScript action to calculate 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
Option | Description |
---|---|
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:
lines_input > 100
or
true
or
parent_workflow.getVariable("INPUT_DIRECTORY").equals("/tmp");
The following variables are available for the expression:
Variable | Description |
---|---|
| Number of errors in the previous workflow action (long). |
| Number of rows read from database or file (long). |
| Number of rows written to database or file (long). |
| Number of rows updated in a database table (long). |
| number of rows read from a previous pipeline transform (long). |
| Number of rows written to a next pipeline transform (long). |
| Number of files retrieved from an FTP server (long). |
| The exit status of a shell script (integer). |
| The workflow action number of the previous workflow action (long); increments at every next workflow action. |
| use if Hop runs on Windows (boolean). |
| The parent workflow of the current workflow action. |
| The current workflow action. |
Variables
Here is how you can evaluate the content of a variable string:
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:
parent_workflow.setVariable("NR_OF_ROWS", "1000000");
For example you can do something like the following to manipulate variables within this workflow action:
useDate = parent_workflow.getVariable("use_date").equals("1");
if (useDate == 0) {
date = new java.util.Date();
date.setDate(date.getDate()-1); //Go back 1 full day
dateFormat = new java.text.SimpleDateFormat("yyyyMMdd");
newDateStr = dateFormat.format(date);
parent_workflow.setVariable("start_date", newDateStr);
}
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:
Expression | Alternative | Data type | Meaning |
---|---|---|---|
| boolean | true if the previous workflow action was executed successfully, false if there was some error. | |
| exit_status | int | exit status of previous shell script workflow action |
| nr | int | The action number is increased every time a workflow action is executed. |
| errors | long | the number of errors, also available as variable “errors”. |
| lines_input | long | The number of rows read from a file or database. |
| lines_output | long | The number of rows written to a file or database. |
| lines_read | long | The number of rows read from previous transforms. |
| lines_updated | long | The number of rows updated in a file or database. |
| lines_written | long | The number of rows written to next transform. |
| lines_deleted | long | The number of deleted rows. |
| lines_rejected | long | The number of rows rejected and passed to another transform via error handling. |
| List<RowMetaAndData> | The result rows, see also below. | |
| boolean | Flag to signal if the previous previous workflow action stopped or not. | |
| List<ResultFile> | The list of all the files used in the previous workflow action (or actions). | |
| files_retrieved | int | The number of files retrieved from FTP, SFTP, etc. |
| String | The log text of the execution of the previous workflow action and its children. | |
| 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:
var firstRow = rows[0];
firstRow.getString("name", "").equals("Foo")
This script will follow the green workflow hop is the expression evaluates to true. This happens if field "name" contains String "Foo".