Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

About Set Variable

The Set Variable (set-variable) component is for creating or updating a variable to store values for use within the flow of a Mule app. 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 for complex scenarios.

Field Usage Description

Variable Name (variableName)

Required

Name of the variable. Names can only include numbers, characters, and underscores. For example, hyphens are not allowed in the name.

Value (value)

Required

Value for the variable, which can be a string or a DataWeave expression.

Mime Type (mimeType)

Optional

Sets the variable MIME type, such as text/plain or application/json.

Encoding (encoding)

Optional

Sets the variable encoding, such as ISO 10646/Unicode(UTF-8).

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

In this topic: