Nav

About Creating Variables (Design Center)

About Variables

Variables are used to store per-event values for use within a flow of a Mule app. The stored data can be any supported data type, such as an object, number, or string. It is also possible to store the current message (using the message keyword), the current message payload (using the payload keyword) or just the current message attributes (using the attributes keyword). You can even use a DataWeave expression as the value. However, the keyword vars (for example, vars.someOtherVar) is not allowed.

You can create, or update, variables in these ways:

  • Using the Set Variable component.

  • Using a Target Variable from within an operation, such as the Read operation to the File connector or the Store operation to the Database connector.

There are other ways of setting variables; however, they are used less much less frequently.

After creating a variable, you can access and use it within the scope of a Mule flow where you created it, which includes any flows that are joined with it through a Flow Ref component.

  • vars: Keyword for accessing a variable, for example, through a DataWeave expression in a Mule component, such as the Logger, or from an Input or Output parameter of an operation. If the name of your variable is myVar, you can access it like this: vars.myVar

Note that a variable in the Mule event is different from variables defined in a DataWeave script, which are local to the script and not accessible outside of it.

About the Set Variable Component

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

For more information, see "About Set Variable", which is linked to from the See also section at the end of this topic.

Enhancing Data with Target Parameters

When creating a flow in a Mule app, you might need to store data in a variable so that any component in the flow can use it. Non-void operations (such as the Read operation to the File connector) can store the message data that they return in a variable. Once defined, variables created with the target parameter are available for use within the flow, and you can access them like you access any other variable.

You often define variables through these parameters:

  • Target (target): Name of the variable in which you want to store message data. Names can only include numbers, characters, and underscores. For example, hyphens are not allowed in the name.

  • Target Value (targetValue): Value of the data to store in the target variable. By default, the value is the message payload (payload). The field accepts any value that a variable accepts: any supported data type, DataWeave expressions, the keywords payload, attributes, and message, but not the keyword vars.

It is important to understand that setting a target variable changes the typical course of the message through the flow. Normally, an operation outputs a different message than it receives as input. However, when you set a variable from an operation, the operation outputs to the next component in the flow the same message that it received as input. For example, assume you have a flow consisting of component A, followed by component B with a target value of myMessage, and then component C. In this case, component C will receive the same message that B received from A.

In this image, the Read operation of an FTP connector stores its payload in the variable myVar:

ftp read output target
  1. The Output fields for the target variable are in the Advanced section of the component card, as is the case for all components that have these fields.

  2. The name myVar has been specified as the name of the target variable. There is no default name. The target variable stores the payload by default. You need to specify a target value only if you want to store something other than the full payload.

To access the content of myVar in another operation in the flow, you would use the expression myVar.payload.

For a list of the DataWeave functions that you can use in variable expressions, see "Core" in the DataWeave Function Reference. "Core" is linked to from the See also section at the end of this topic.

See also

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.

+