---
schema_version: "1"
id: e834637e2ea35b22e0fddf09f28c6e6466fba75644e8ba5f9588e77f6370f001
title: Set Variable (<set-variable/>)
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/anypoint-code-builder/acb-component-set-variable.md
canonical_url: https://docs.mulesoft.com/anypoint-code-builder/acb-component-set-variable
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 86b80b4f6d66ac6c342436af38140d9233b48d68d9e5780aacb628531f9fea94
description: MuleSoft Documentation Site
latest: true
---
---
product: Anypoint Code Builder
version: master
---

# Set Variable (<set-variable/>)

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

Creates or updates a [Mule variable](https://docs.mulesoft.com/mule-runtime/latest/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/latest/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.

Don’t use Set Variable for complex expressions or transformations. Instead, use the component for
simple operations, such as selections, and use the Transform component for complex scenarios.

## Component XML

This component supports the following XML structure:

```xml
<set-variable
  variableName="myVar"
  value="myVarValue"
  doc:name="Set variable"
  doc:id="mrsngz" />
```

Set Variable (`<set-variable/>`) attributes are configurable through the UI and XML.

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Attribute XML</th><th>Description</th></tr></thead><tbody><tr><td><p><strong>Set variable</strong> (default)</p></td><td><p><code>doc:name</code></p></td><td><div><div><p>Editable name for the component to display in the canvas.</p></div></div></td></tr><tr><td><p>N/A</p></td><td><p><code>doc:id</code></p></td><td><div><div><p>Automatically generated identifier for the component.</p></div></div></td></tr><tr><td><p><strong>Value</strong></p></td><td><p><code>value</code></p></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/2.4/dataweave-cookbook-writer-prop-mule">Use Dynamic Writer Properties</a>. This attribute is required.</p></div></div></td></tr><tr><td><p><strong>Variable name</strong></p></td><td><p><code>variableName</code></p></td><td><div><div><p>Name of the variable, which can be a string or a DataWeave expression. Variable names are must include only numbers, characters, and underscores. This attribute is required.</p></div></div></td></tr><tr><td><p><strong>Encoding</strong></p></td><td><p><code>encoding</code></p></td><td><div><div><p>Encoding of the payload that this component outputs.</p></div></div></td></tr><tr><td><p><strong>Mime type</strong></p></td><td><p><code>mimeType</code></p></td><td><div><div><p>MIME type of the payload that this component outputs. For more information, see <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-formats#reader_writer_properties">Using Reader and Writer Properties</a>.</p></div></div></td></tr><tr><td><p><strong>Parameters</strong></p></td><td><p>N/A</p></td><td><div><div><p>Appends a key-value pair for a reader property to the value of the <code>outputMimeType</code> attribute. Multiple key-value pairs are allowed. Reader properties are specific to the MIME type. For example, see the <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-formats-json#properties">JSON reader properties</a> for <code>application/json</code>. To find other reader properties, navigate to a supported format from the <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-formats">DataWeave format documentation</a>, and see <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-formats#reader_writer_properties">Using Reader and Writer Properties</a>.</p></div></div></td></tr></tbody></table>

## 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`. For
example, you can access a variable named _lastMessage_ with `vars.lastMessage`. You can set
variables in a Transform component, and also many connectors and event processors have a Target
variable that you can set.

## Examples

### Example: Setting a Variable With a String Value

The following example assigns a String value to a variable:

```xml
<set-variable
  value="My First Variable"
  variableName="myVar" />
```

### Example: Setting the Result of a DataWeave Expression as a Variable

The following example assigns the result of a DataWeave expression to a variable:

```xml
<set-variable
  value=#[max([1,2,3] ++ [3,4,5])]
  variableName="myResult" />
```

### Example: Setting a Variable to the Message Payload

The following example assigns the message payload to a variable:

```xml
<set-variable
  value=#[payload]
  variableName="myPayload" />
```

### Example: Setting a Variable to the Message Attributes

The following example assigns the message attributes to a variable:

```xml
<set-variable
  value=#[attributes]
  variableName="myAttributes" />
```

### Example: Setting a Variable With a Map Value

The following example assigns a map value to a variable:

```xml
<set-variable
  value="{ 'name' : 'Ana', 'office' : 'BA' }"
  variableName="employee"
  mimeType="application/json"
  encoding="UTF-8"/>
```

### Example: Setting a Variable to a Java Object

The following example assigns a Java object to a variable by using a DataWeave script:

```xml
<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"}]'
  variableName="Variable to Java Object"/>
```

## See Also

- [Transform Component](https://docs.mulesoft.com/anypoint-code-builder/acb-component-transform)

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

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