> 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/service-backup-of-types/define-variable.md).

# Define Variable

This type of action allows you to initialize or update a variable that will be available in the whole process context. This variable can be used in subsequent steps/connectors.

When the action is added, it offers several settings depending on the main setting: **Variable type** (1).

![](/files/5abd4c95a78af04fd5b221acd891ca30ee7eda1d)

A variable can be one of 4 types:

* Text
* Query
* Apex
* JSON

## Text

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

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

![](/files/1387b2b0f4988a04e4754b827440803386540f1b)

## Query

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

{% stepper %}
{% step %}

### Select the object

Select the Query variable type, then choose the object (4) whose fields will be used to set values to variables.

![](/files/f5ad3f384aa12b852395435e91c480e07c81549e)
{% endstep %}

{% step %}

### Add a field

Click the **Add field** button in the Fields section (5).

![](/files/41939aa88956ddeea269e1fc91e46e52aee3b1ba)
{% endstep %}

{% step %}

### Choose the field and variable name

Choose the Field name (6) and set the new Variable name (7).

![](/files/fb7497c32ff0b02ca8fc5c85d35df036bb387171)
{% endstep %}

{% step %}

### Add a condition if necessary

Click the **Add Condition** button (8), then specify Name, Operator, and Value.

![](/files/2232506a643ab59f7cd36a1212e59044431764ee)

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

## Apex

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

* Select Behavior (9) to define whether only one variable or several variables will be returned.

![](/files/d3e3d6494b236c4a99ffc190f6b0a9783ac4f58e)

**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.

* Provide the variable name and class name (10, 11).

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

Example of an Apex class that will set a variable value:

{% code overflow="wrap" %}

```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 == '4') 
            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" %}

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

{% endcode %}

Specify necessary parameters (12). For example, switch to Advanced mode, choose Context object, and choose Context object item.

![](/files/d68b0989968d4cfed340a4193a8702947724b2f2)

{% hint style="info" %}
In the above example, the variable value will be set to `'Hello world'` because 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.

Example:

{% code overflow="wrap" %}

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

{% endcode %}

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

## 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, a 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 (12). When it is selected, the system will suggest names of variables defined by that action (13). When selected, an Output Variable will be available to use as merge field `$Output.variableName` (14).

![](/files/f49ee2de64c1d643528328c2e02d93d04a592dd9)

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