Contact Us 1-800-596-4880

Set Variable (<set-variable/>)

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Creates or updates a Mule variable to store values for use within the flow of a Mule app.

A Mule variable is part of the 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:

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

Attribute Name Attribute XML Description

Set variable (default)

doc:name

Editable name for the component to display in the canvas.

N/A

doc:id

Automatically generated identifier for the component.

Value

value

Value for the variable, which can be a string or a DataWeave expression. For an example that uses an expression, see Use Dynamic Writer Properties. This attribute is required.

Variable name

variableName

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.

Encoding

encoding

Encoding of the payload that this component outputs.

Mime type

mimeType

MIME type of the payload that this component outputs. For more information, see Using Reader and Writer Properties.

Parameters

N/A

Appends a key-value pair for a reader property to the value of the outputMimeType attribute. Multiple key-value pairs are allowed. Reader properties are specific to the MIME type. For example, see the JSON reader properties for application/json. To find other reader properties, navigate to a supported format from the DataWeave format documentation, and see Using Reader and Writer Properties.

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

The following examples set the value of a Mule variable to a string, to a DataWeave operation’s result, to the message payload, to the message attributes, to a map value, and to a Java object.

Example: Setting a Variable With a String Value

The following example assigns a String value to a variable:

<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:

<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:

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

Example: Setting a Variable to the Message Attributes

The following example assigns the message attributes to a variable:

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

Example: Setting a Variable With a Map Value

The following example assigns a map value to a variable:

<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:

<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"/>