# Rule Variables

Rule variables simplify editing when working with values that appear multiple times across your rules.

**Rule variables work identically across all rule types** - Decision Tables, Decision Trees, and Scripting Rules - though the syntax for using them varies by rule type.

{% hint style="info" %}
Rule variables are version-specific. Each version of a rule maintains its own set of variables.
{% endhint %}

{% hint style="info" %}
In this guideline on how to create rule variables, you need to have knowledge of [Decision Table](https://docs.decisionrules.io/doc/rules/decision-table) and [Scripting Rule](https://docs.decisionrules.io/doc/rules/scripting-rule)
{% endhint %}

## **Creating and Managing Rule Variables**

Rule Variables are managed in the **left panel** of the rule editor. Here you can add, edit, or delete variables.

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FAT2FqRnEzzbaO6goYDn0%2FScreenshot%202026-02-09%20at%2011.53.59.png?alt=media&#x26;token=ed4be6d2-b370-47cd-9532-f3e7ccc351bb" alt=""><figcaption></figcaption></figure>

#### **To create a new variable:**

1. Enter a **Name** (must be unique per rule).
2. Set a **Value** (number, text, or boolean).
3. Click the **Save** button or click **Enter.**

{% hint style="success" %}
Create variable names **without spaces and special symbols**, even though they are technically allowed.
{% endhint %}

#### **To edit a variable:**

1. Click the **Edit** button next to the variable.
2. Modify the value.
3. Click the **Save** button or click **Enter.**

{% hint style="warning" %}
Save your changes – otherwise, the changed value of the rule variable will not be updated.
{% endhint %}

#### **To delete a variable:**

Click the **Delete** button next to the variable.

### Supported Value Types

Rule Variables support the following data types:

#### Simple Values

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FxYlwyjMWJrqMOkFkBF9R%2FScreenshot%202026-02-09%20at%2012.16.34.png?alt=media&#x26;token=477e74dd-9e30-4881-b83d-7bc14645cc0a" alt=""><figcaption></figcaption></figure>

```javascript
{ "output": [ 0.21, 18, 30, "EUR", "Prague", "Premium", true, false ] }
```

#### Arrays

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2Fj819xdG6O0FxeL3S4GYV%2FScreenshot%202026-02-09%20at%2012.21.08.png?alt=media&#x26;token=e0614814-4f51-4adc-a94e-3b1cb60375b8" alt=""><figcaption></figcaption></figure>

```javascript
[
    { "output": [15, 30, 45, 60] },                    // priceList
    { "output": ["Junior", "Standard", "Senior"] },    // categories
    { "output": [1, 2, true, "text"] }                 // mixedArray
]
```

#### JSON Objects

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FBF3aJJtC9orIVCcisFk0%2FScreenshot%202026-02-09%20at%2012.36.40.png?alt=media&#x26;token=6f917701-a3db-41a7-ae89-ed6f5e11810b" alt=""><figcaption></figcaption></figure>

```javascript
[
  { "output": { "id": 10, "name": "Peter", "occupation": "Developer" } },
  { "output": { "tax": 0.21, "currency": "EUR", "minAmount": 50 } }
]
```

{% hint style="warning" %}
While rule variables support complex objects and arrays, we **do not recommend using overly complicated rule variables**. Keep variables simple and focused.
{% endhint %}

### What Rule Variables CANNOT Do

Rule variables have important limitations that you should understand:

#### Reference other values

Rule variables are **static values only**. You cannot reference one variable from another or perform calculations between them.

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FmMFeKyFvbc84J1oChCSP%2FScreenshot%202026-02-09%20at%2012.47.24.png?alt=media&#x26;token=bf2d0772-4d81-4984-9fac-1f1b415a47d9" alt=""><figcaption></figcaption></figure>

```javascript
[
  { "output": 30},
  { "output": "{price1} * 2" } // Returns literal string, NOT 60
]
```

While you might think `{input.field}` syntax would work based on other features, **rule variables do NOT support input placeholders**. The curly braces are treated as literal text.

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FZjGf4QHfy7IMQG1ASymJ%2FScreenshot%202026-02-09%20at%2012.44.19.png?alt=media&#x26;token=50529859-d3d9-4ba2-a63b-d3f4d25761de" alt=""><figcaption></figcaption></figure>

```javascript
[
    { "output": "[1, {input.value}, true, \"text\"]" },
    { "output": "{\"tax\":{income.tax},\"currency\":\"EUR\",\"minAmount\":50} }
]
```

#### Execute functions

Functions stored in rule variables are **NOT executed** - they are returned as strings.

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2Fsw3YVs6oYaP8b6VydWTI%2FScreenshot%202026-02-09%20at%2012.48.38.png?alt=media&#x26;token=1ebcc906-aba0-4392-8428-19b205e11929" alt=""><figcaption></figcaption></figure>

```javascript
[
  { "output": "BTW(10, {input}, 40)" },
  { "output": "GT(30, 50)" },
  { "output": "SUM(10, 20)" }
]
```

{% hint style="info" %}
If you need to use functions, write them directly in table.
{% endhint %}

## How to use Rule Variables

### **Rule Variables in Decision Tables**

There are several ways to use Rule Variables in Decision Table Designer:

#### **As the Column Condition**

In the column header, you can use the variable name directly.

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FroNV0JE2u0Q72eTMAla0%2FScreenshot%202026-02-09%20at%2014.17.12.png?alt=media&#x26;token=ce81cd9f-5565-45b2-920a-156b197a0286" alt=""><figcaption></figcaption></figure>

#### As the Comparison Value

In the condition cell, you will see reference with the `A` prefix

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FFQWtA4uWLI5yAANRPHas%2FScreenshot%202026-02-09%20at%2014.12.19.png?alt=media&#x26;token=11e86e48-091f-482f-9e1c-62303347ed17" alt=""><figcaption></figcaption></figure>

**Both approaches achieve the same result** - comparing input data against your rule variable. Choose the style that makes your table more readable.

{% hint style="warning" %}
The column header is always the **left side** of the comparison. When you move a variable between the column header and the condition cell, you must **reverse the operator** to maintain the same logic.
{% endhint %}

#### As Part of the Function

Rule variables can be used inside functions and mathematical expressions, just like any other value (input fields, column values, or static numbers).

<figure><img src="https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FG9md0oeaI95kvtaHqX8I%2FScreenshot%202026-02-09%20at%2013.39.59.png?alt=media&#x26;token=9723c8ee-8429-4309-af7c-f9a24bd61aff" alt=""><figcaption></figcaption></figure>

This is useful when you want to calculate output without hardcoding them in each cell. If value change, you only update the variable once.

{% hint style="info" %}
Exactly the same use of Rule Variable applies to Decision Tree.
{% endhint %}

### **Rule Variables in Scripting Rule**

Usage of Rule Variables in Scripting Rules is super easy. All values are stored in the `ruleVariables` object that is exposed in the script. You can use these stored values everywhere in the script (as inputs for your function or as inputs for `DR.solve()` function).

You can access your Rule Variables with the dot notation same as with input and output. To get the value of a rule variable, you write `ruleVariables.<nameOfYourRuleVariable>`

```javascript
const foo = ruleVariables.foo;
const bar = ruleVariables.bar;

output.Output = `Values of Rule Variables are: ${foo} and ${bar}`;

return output;
```

{% hint style="danger" %}
You cannot set `ruleVariables` from script itself or overwrite existing Rule Variables.
{% endhint %}
