# UserVariable task

A UserVariable task gives a name to a value, so that the tasks behind it can use it as `{name}`. It is how a job remembers something: a folder that several tasks use, a date worked out once, a counter, a flag that says "the fetch worked".

It is also where most of a job's small calculations happen, because the value can be built with [functions](Jobs-Functions.md).

## Fields

| Field | What it does |
|---|---|
| **Variable Name** | the name, without braces: `target` |
| **Variable Value** | the value. Text, [variables](Jobs-Variables.md), functions. For a name with "password" in it, the field hides what is typed. |
| further names and values | one task can set many variables; they are set in order, and a later one can use an earlier one |
| **Item used for variable reference**: **Last item** / **First item** | when the value uses something of a file, such as `{name}`: whose. The list may hold many files, and a variable of the run has one value. |
| **Remove Item** | takes that item off the list afterwards |

## Where a variable lives

The beginning of the name decides:

| Name | Lives | Example |
|---|---|---|
| anything else | in this run, for every task and every branch | `target` |
| `ITEM_…` | on **each item**: every file gets its own value, worked out from its own `{name}`, `{size}` and so on | `ITEM_month` with the value `{-MM}` gives every file the month it was last changed |
| `GLOBAL_…` | on the server until it is restarted, for every job | `GLOBAL_last_export` |
| `PERSIST_…` | for good, in `persistent_job_variables.XML`. An empty value removes it. | `PERSIST_invoice_no` |
| `USER_INFO_…` | in the session of the user whose event started the job | |

An `ITEM_` variable is the one to sort by, to group by in a [Jump](Task-Jump.md), or to use in a destination: it differs from file to file. In its value, `{index}` is the item's place on the list, from 0.

`GLOBAL_` and `PERSIST_` variables reach from one job into another, so they need a right of their own for admins with limited rights.

## Examples

| Name | Value | Gives |
|---|---|---|
| `today` | `{yyyy}{MM}{dd}` | the date, fixed for the whole run, also when the run goes past midnight |
| `target` | `file:///data/archive/{yyyy}/{MM}/` | one place to change the folder |
| `ITEM_customer` | `{split_start:_:0}{name}{split_end}` | for `ACME_orders_17.csv`: `ACME`, on each file |
| `PERSIST_run_no` | `{increment_start}{PERSIST_run_no}{increment_end}` | a counter that grows by one with every run. Set it to 0 once by hand. |
| `fetched` | `yes` | a flag for a later Jump or the stop condition of a Loop |
| `GLOBAL_partner_down` | `true` | a note for other jobs |

## Functions that only work here

A value can do more than be a text:

| Value | Does |
|---|---|
| `{save_list_start}mylist{save_list_end}` | keeps a copy of the list of items, as far as the task's filter matches, under the name `mylist` |
| `{load_list_start}mylist{load_list_end}` | replaces the list of items with the kept one |
| `{add_list_start}mylist{add_list_end}` | adds the item in hand to the kept list |
| `{json_parse_start}{file_contents}{json_parse_end}` | reads JSON and sets a variable on the item for each of its values, one level deep |
| `{xml_parse_start}…{xml_parse_end}` | the same for XML |
| `{properties_parse_start}…{properties_parse_end}` | the same for lines of `name=value` |

The lists are how a job puts files aside and brings them back: save the list after the Find, work on something else, load it again.

The parsers are how the answer of an [HTTP](Task-HTTP.md) task is used: the answer is JSON, the parser makes `{status}` and `{id}` of it, and a Jump decides by them.

What a parser reads cannot overwrite what an item is: a value named `url` or `the_file_path` in the JSON is refused, with a line in the log, so that data cannot point a later task at another file.

## What it hands on

The items it was given, unchanged, unless a list was loaded or **Remove Item** is ticked.

## Things to know

- **With an empty list** the task still runs once. A variable that uses nothing of a file can be set at the very start of a job.
- **Branches share variables.** Two branches that run at once and set the same name overwrite each other: see [How a job flows](Jobs-Flow.md#branches).
- **Names.** Use letters, digits and the underscore. A name that is also the name of something an item has, such as `name` or `size`, hides the item's value for the tasks that follow: avoid those.
- **What a value brings in stays data.** A variable set from a file's name that happens to hold braces is not worked out a second time.
- **Seeing a variable.** A [breakpoint](Jobs-Breakpoints.md) behind the task shows the run's values, or put the variable into a [WriteFile](Task-WriteFile.md) or the subject of a test mail.
