# 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

| Option      | Description                      |
| ----------- | -------------------------------- |
| Action name | The name of the workflow action. |
| JavaScript  | The JavaScript field.            |

### Examples

```highlight
typeof parent_workflow.getVariable("DETAIL_ID_DATASOURCE_ID") != "undefined" && parent_workflow.getVariable("DETAIL_ID_DATASOURCE_ID").toString() == ""
```

```highlight
parent_workflow.getVariable("API_RETRY_COUNTER") != parent_workflow.getVariable("API_RETRY_LIMIT")
```

### Troubleshooting

* Remove as many line returns as possible in the JavaScript.

### 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:

```highlight
lines_input > 100
```

or

```highlight
true
```

or

```highlight
parent_workflow.getVariable("INPUT_DIRECTORY").equals("/tmp");
```

The following variables are available for the expression:

| Variable          | Description                                                                                                  |
| ----------------- | ------------------------------------------------------------------------------------------------------------ |
| `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:

```highlight
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:

```highlight
parent_workflow.setVariable("NR_OF_ROWS", "1000000");
```

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

```highlight
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                                                                                                                                                   |
| --------------------------------------- | ---------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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:

```highlight
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".
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.primeur.com/data-shaper-1.21/knowing-the-data-shaper-designer/workflows/actions/eval.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
