Nav

To Create and Populate a Variable (Design Center)

Variables are a way to internally pass data along a flow to the next component. Variables belong to a Mule event, and can only be accessed through the Mule event in a flow; variables do not persist anywhere else in the Mule application.

Create a Variable With an Operation

  1. Open any component that isn’t acting as a flow trigger (ex: a HTTP Request component).

  2. Select the Advanced tab of this component.

  3. In the Target Variable field, write the name of your new variable.

    to create and populate a variable 3a582

This creates a new variable and populates it with data that’s returned by the operation. For example: on an HTTP Request operation, the response of the sent HTTP Request is recorded onto the variable as a Mule message object (which will overwrite the payload) instead of overwriting the main Mule message.

As a reminder, a Mule message object contains:

  1. An attributes object that contains metadata about the nature of the data request.

  2. A payload object that contains the actual data.

You can access the saved payload downstream through using the syntax:

variables.MyVariableName.payload

Create a Variable With the Transform Element

  1. Add a new Transform Component to your flow and open its properties

  2. On the output section, click the dropdown next to the output label (it should display "Output Payload" by default), and select Add Transformation.

    to create and populate a variable 02fd0

  3. In the Type field, pick "Variable", and then give a name to your new variable.

    to create and populate a variable 0de76

  4. The Transform component now displays the transformation that outputs into your new variable.

    Unlike other components, any variables created in the Transform component are not Mule message objects. These variables will directly contain the data from your transformation.
    If you don’t want to process an extra transformation that outputs payload in this step, then you should delete the default transformation that the component created when you added it. To do so, open the dropdown next to the output label and click the trash can icon to create and populate a variable 85dfe next to Payload.

    to create and populate a variable a9b3c

  5. Define a Data Type for your variable

  6. Construct a Mapping of values from the input into your new variable.

Your new variable is created and populated with data from the input. The following components on your flow should have access to it.

See also

  • If you need a variable that persists its value in between executions of the flow, you need to use the Object Store Connector.

  • See Mule Concepts for a better understanding of how variables fit into the Mule event structure. Unlike in 3.x versions of Mule runtime, only one type of variable exists in Mule 4.x.

  • See DataWeave cookbook to learn how to extract a variable from the Mule event.