> 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/processes/process-actions/process-actions-types/define-variable.md).

# Define Variable

### Generic Process Action properties

This action has generic properties, as other process actions:

* **Name** - give action a name in order to search for it easy, otherwise system will use ID of this Action.
* **Step** - the step action is added at. This property can't be edited manually, but it will be updated if move this action to other step or connector.
* **Type** - main property which determines what Action will do and it's configuration. Once action is created, type can't be changed.
* **Status** - you can activate or deactivate an action temporary (e.g., for debug).
* **Start Type** - can be set to "Before step" or "After step".&#x20;

{% hint style="info" %}
This option is available only when action is added to the step. If action is added to a connector, it will be executed like "After step", where step is the outgoing step of this connector.
{% endhint %}

* **Execute** - specify if Action will be executed always or when some conditions are matched.
* **Description** - used internally only for admin's goals.

### 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="/spaces/kkIfnKKNSULyGZ085PIG/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="/spaces/kkIfnKKNSULyGZ085PIG/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="/spaces/kkIfnKKNSULyGZ085PIG/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="/spaces/kkIfnKKNSULyGZ085PIG/files/DZCXHYcbFsB9mmDak0dL" alt=""><figcaption></figcaption></figure>

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

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

* Choose the Field and set the new Variable name:

<figure><img src="/spaces/kkIfnKKNSULyGZ085PIG/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="/spaces/kkIfnKKNSULyGZ085PIG/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="/spaces/kkIfnKKNSULyGZ085PIG/files/a8ZwPQ38LoTAyX8b5zBb" alt=""><figcaption></figcaption></figure>

<figure><img src="/spaces/kkIfnKKNSULyGZ085PIG/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="/spaces/kkIfnKKNSULyGZ085PIG/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.

![](/spaces/kkIfnKKNSULyGZ085PIG/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.
