---
schema_version: "1"
id: fda47925701160b9565c830f0b30ad32e2b550a5a53fdda7b99356528772adcc
title: Set Variable Transformer
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/4.11/variable-transformer-reference.md
canonical_url: https://docs.mulesoft.com/mule-runtime/4.11/variable-transformer-reference
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 26aa818f9be3b5997904a0293c0803a3b930519e50db11ec1e849c34d3fed733
description: MuleSoft Documentation Site
release: "4.11"
latest: false
---
---
product: Mule Runtime
version: 4.11
keywords: anypoint studio, studio, mule, variable transformer, variables, set variable, edit variable, remove variable
---

# Set Variable Transformer

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The Set Variable (`set-variable`) component is for creating or updating a
[Mule variable](https://docs.mulesoft.com/mule-runtime/4.11/about-mule-variables) to store values for use within the flow of a Mule app. A Mule
variable is part of the [Mule event](https://docs.mulesoft.com/mule-runtime/4.11/about-mule-event). You can store simple literal values such as
strings or messages, message payloads, or attribute objects. For example, you might store the
original payload of a message (before it is processed) so you can use it later in the flow or within
an error handler.

The `set-variable` component is not recommended for complex expressions or transformations. You
should instead use it for simple ones, such as selections, and use the
[Transform Component](https://docs.mulesoft.com/mule-runtime/4.11/transform-component-about) for complex scenarios.

## Reference

Set Variable provides a way to set the name and value of Mule variable, along with configurable
properties.

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Field</th><th>Usage</th><th>Description</th></tr></thead><tbody><tr><td><div><div><p>Variable Name (<code>variableName</code>)</p></div></div></td><td><div><div><p>Required</p></div></div></td><td><div><div><p>Name of the variable, which must be a string. Variable names can include only numbers, characters, and underscores. For example, hyphens are not allowed in the name.</p></div></div></td></tr><tr><td><div><div><p>Value (<code>value</code>)</p></div></div></td><td><div><div><p>Required</p></div></div></td><td><div><div><p>Value for the variable, which can be a string or a DataWeave expression. For an example that uses an expression, see <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-writer-prop-mule">Use Dynamic Writer Properties</a>.</p></div></div></td></tr><tr><td><div><div><p>Mime Type (<code>mimeType</code>)</p></div></div></td><td><div><div><p>Optional</p></div></div></td><td><div><div><p>Sets the variable MIME type, such as <code>text/plain</code> or <code>application/json</code>.</p></div></div></td></tr><tr><td><div><div><p>Encoding (<code>encoding</code>)</p></div></div></td><td><div><div><p>Optional</p></div></div></td><td><div><div><p>Sets the variable encoding, such as <code>ISO 10646/Unicode(UTF-8)</code>.</p></div></div></td></tr></tbody></table>

> [!WARNING] The `mimeType` and `encoding` attributes do not affect a DataWeave expression used as
> `value`. They only affect the output value stored. If a transformation is required, the DataWeave
> expression must contain an explicit output directive.

## Examples

This example sets the variable to a string:

- Name = `myVar`

- Value = `my first variable`

This example sets the variable by using a DataWeave operation that results in a value of `5`:

- Name = `myVar`

- Value = `#[max([1,2,3] ++ [3,4,5])]` in Anypoint Studio.

This example sets the variable to the message payload:

- Name = `myVar`

- Value = `payload` in Design Center, `#[payload]` in Anypoint Studio.

This example sets the variable to the message attributes:

- Name = `myVar`

- Value = `attributes` in Design Center, `#[attributes]` in Anypoint Studio.

This example sets the variable to the entire message:

- Name = `myVar`

- Value = `message` in Design Center, `#[message]` in Anypoint Studio.

This XML example sets a variable that takes a map as a value:
`<set-variable variableName="employee" value="{ 'name' : 'Ana', 'office' : 'BA' }" mimeType="application/json" encoding="UTF-8"/>`

This example sets the same variable using selectors in a DataWeave script. It assumes the `name`
attribute is available as input to Set Variable:
`<set-variable variableName="employee" value="#[output application/java --- payload.name]"/>`

These examples set the variable to a Boolean value, `true`:

- Name = `myVar`

- Value =

  - `true` in Design Center, `#[true]` in Anypoint Studio

  - `true as Boolean` in Design Center, `#[true as Boolean]` in Anypoint Studio

  - `(1 + 1 == 2)` evaluates to `true` in Design Center, `#[(1 + 1 == 2)]` evaluates to `true` in
    Anypoint Studio.

This example sets a variable to a Java object by using a DataWeave script. The script outputs the
object in Java format:
`<set-variable value='#[%dw 2.0 output application/java --- { name: "Tomo", lastName: "Chibana", expirationDate: now(), salesRepr: { name: "Mariano", lastName: "de Achaval", } } as Object {class: "Customer"}]' doc:name="Set Variable" variableName="Variable to Java Object"/>`.

For more information about the Java data format, refer to the
[Java Format examples](https://docs.mulesoft.com/dataweave/latest/dataweave-formats-java#format_examples) documentation.

To display the value of a variable through the Logger component in Design Center, you might need to
use the Anypoint Studio syntax, for example, `#[vars.myVar]` instead of `vars.myVar`.

## Accessing Variables in Other Event Processors

Set Variable sets a variable in the current Mule event, and the variables then travel with the Mule
event to downstream event processors. You can access any variable with DataWeave using `vars`. So if
you set a variable named lastMessage, you can access it as `vars.lastMessage`. You can set variables
in a Transform Message component, and also many connectors and event processors have a Target that
can be set in the Advanced tab. These all set flow variables and they are accessed the same way,
through the keyword `vars.`.

## See Also

- [Variables in Mule Events](https://docs.mulesoft.com/mule-runtime/4.11/about-mule-variables)

- [Enrich Data with Target Variables](https://docs.mulesoft.com/mule-runtime/4.11/target-variables)
