> For the complete documentation index, see [llms.txt](https://docs.work-relay.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.work-relay.com/automations/global-actions-and-action-groups/types/define-variable.md).

# Define Variable

### Generic Global Action properties

This action has generic properties, as other global actions:

* **Name** - give Action a name in order to search for it easy, otherwise system will use ID of this Action.
* **Type** (Rule or Action) - this is mandatory. Select "Action".
* **ID** - given by system after saving a new Action first time.
* **Status** - you can activate or deactivate an action temporary (e.g., for debug).
* **Context Object Type** - a data source which fields can be used in Action [conditions](/automations/queries-and-conditions/working-with-conditions/conditions-overview.md) and properties.
* **Action Type** - main property which determines what Action will do and it's configuration. [Explore all action types here](/automations/global-actions-and-action-groups/types.md).
* **Execute** - specify if Action will be executed always or when some conditions are matched.&#x20;

<figure><img src="/files/Wyr0epEhpQbqgP9w7cC9" alt=""><figcaption></figcaption></figure>

### Overview

This type of action allows you to initialize or update a variable that will be available in result handlers and other actions.

When the action is added, it offers several settings depending on the main property **Variable type:**

<img src="/files/sWQedhIaymc8gp1AWfYz" alt="" class="gitbook-drawing">

A variable can be one of these types:

* Text
* Query
* Apex
* JSON

### Text

The variable will have a string data type. It requires the **Name** property. **Value** is optional and will be equal to an empty string if not specified:

<figure><img src="/files/LqjLRD1HVL0eIwIsKKzV" alt=""><figcaption></figcaption></figure>

Name can be specified as regular text or set dynamically. You can use Variables and Merge Fields to set the name:

<figure><img src="/files/iCshW41TRatNSMzGIdZ5" alt=""><figcaption></figcaption></figure>

### Query

This is a way to specify several variables together based on Data Source Objects record fields.

* Select the Query variable type, then choose the object which fields will be used to set values to variables:

<figure><img src="/files/DZCXHYcbFsB9mmDak0dL" alt=""><figcaption></figcaption></figure>

* Click the **Add field** button in the Fields section:

<figure><img src="/files/LcU5CpDs2rZCJVbbTw7L" alt=""><figcaption></figcaption></figure>

* Choose the Field and set the new Variable name:

<figure><img src="/files/SjmZ8vpoGbqZPdLuTtef" alt=""><figcaption></figcaption></figure>

* Add a condition if necessary. Click the **Add Condition** button, then specify field name, operator, and value. Add more conditions, if necessary, and set a conditions logic:

<figure><img src="/files/ESXqDlEzHWtKB3Lvy6c4" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If your query can return more than one result, specify a **Limit** (default is 1) in order to prevent getting too many entries. Only top 1 entry field values will be used in this Action, so, usually conditions match only one record, but not always.

For more information about using conditions, check [Using Query Conditions.](/automations/queries-and-conditions/working-with-conditions/query-conditions.md)
{% endhint %}

### Apex

The value of the variable or variables will be calculated by program code.

* Select **Behavior** to define whether only one variable or several variables will be returned:

<figure><img src="/files/a8ZwPQ38LoTAyX8b5zBb" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/2QW3jL8TrBqFnhkVEQw5" alt=""><figcaption></figcaption></figure>

**Single Variable** will define one variable.

**Multiple Variables** will define a number of variables. In case of multiple variables, the Apex class must return the variables string in JSON format.<br>

* Provide the **Variable Name** and **Class Name**.

{% hint style="info" %}
To use this kind of variables, you need to have an Apex class implementing the `WorkflowDefineVariableActionInterface` interface.
{% endhint %}

<details>

<summary>Example of an Apex class that will set a variable value</summary>

{% code overflow="wrap" lineNumbers="true" %}

```apex
global with sharing class callApexWrapper implements WR_BPM.WorkflowDefineVariableActionInterface {   
    public Object execute(sObject context,WR_BPM__Flow_Instance_Cursor__c flowInstanceCursor,String operationType,String parameters) { 
        Object result = ''; 
        if(parameters == 'Step4') 
            result = 'Hello world';
        return result;  
    }
}
```

{% endcode %}

The **Execute** method sets the variable based on the **parameters** value. If it equals `'4'`, the variable value will be `'Hello world'`; otherwise, it will be left blank.

{% hint style="info" %}
`result` could be of any type that can be converted to String.
{% endhint %}

Example of the test method that can be used to test the example class:

{% code overflow="wrap" lineNumbers="true" %}

```apex
@isTest static void executeTest(){
   Account accountRecord;
   WR_BPM__Flow_Instance_Cursor__c flowInstanceCursor;
   CallApexWrapper cont = new CallApexWrapper();
   Object result = cont.execute(accountRecord, flowInstanceCursor, '', 'Step4');
   System.assert(result == 'Hello World');
}
```

{% endcode %}

</details>

* Specify necessary parameters. For example, switch to Advanced mode, choose Context object, and choose Context object item:

<figure><img src="/files/742b4gh8Lv1XT33xFM4T" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
In the example above, the variable value will be set to `'Hello world'` if the step name passed as a parameter to an Apex class is `'4'`.
{% endhint %}

### JSON

Select the JSON variable type and provide comma-separated Names and Values.

![](/files/59fbf5d44840888ba23928c78210c460e0d4a084)

Use JSON format to enter any number of variables. Merge fields are allowed.

Example:

{% code overflow="wrap" %}

```json
{"AccountSource": "{Account.AccountSource}", "AccountName":"{Account.Name}"}
```

{% endcode %}

`{$Variables.AccountSource}` will return a value of **Account.AccountSource** field.

### How to work with action results

Variables defined in this action are available to work with in these places:

* On the Global Action Group: as usual variables that are available for any action in this group. If these are new variables, they will be created; otherwise, they will be updated.
* On the form: as fields of the special **Output** context object.

**How to work with the Output context object:**

For example, user has an action that defines some variables:

![](/files/44cf15b9f4d27b99316d525e89960d6f48826fd0)

This action returns its results, and the user needs to use them in another action, such as in a form. The user can specify a Result Handler for the current **Define Variable** action, and the Output context object will be available to select. When it is selected, the system will suggest names of variables defined by that action. When variable is selected, an Output Variable will be available to use as merge field `$Output.variableName`:

<figure><img src="/files/URudXrvMC4B8W6wCOerV" alt=""><figcaption></figcaption></figure>

This merge field can be used as a value of a result handler parameter.
