Store All or Part of the Output of a Card in a Variable (Flow Designer)
You can use a Set Variable card to create a variable that stores all of the output from the preceding card, stores part of the output, or manipulates the output by using functions in a DataWeave expression. You can then reuse that value in a later card by using the variable.
Normally, a card outputs data that is received in its entirety by the next card in the same flow, as depicted by the first flow in this diagram.
The first flow carries out these steps:
-
Card A produces an output.
-
Card B receives the entire output from Card A.
However, when you create a variable with a Set Variable card, both the variable and the output from the previous card are passed to the next card in the flow, as the second flow depicts:
-
Card A produces an output.
-
The Set Variable card creates a variable to store all of the output from Card A, store part of the output, or manipulate the output by using functions in a DataWeave expression.
-
Card B receives the entire set of data from Card A (payload and attributes) and the variable. It outputs its own set of data and the variable. The output from Card A, except for the part that was stored in the variable, is no longer part of the flow
-
Card C receives the entire set of data from Card B and the variable.
For example, in the tutorial "Create Your First Mule Application with Flow Designer", you used a Set Variable card after the trigger card, so that you could save the trigger card’s payload. The reason was that you needed to use information from the payload after using the Choice router, and none of the three possible outputs from the Choice router would preserve that information.
You used the variable, which you named OppDetails
, in the Transform card that followed the Choice router.
OppDetails
highlightedHowever, if you had wanted to use OppDetails
later in the flow, it was still available, as you can see here in the Twilio card, which was the last card in the flow.
OppDetails
variable highlightedTo use the variable in this Twilio card (or in any card where a variable is available, other than the Transform card), you would simply drag it into a field.
If you believe that you no longer need to use a variable after using it one or more times, you can add the Remove Variable card after the card in which the variable is last used.
Procedure
To store a value that is based on the data from a card:
-
Click the plus icon that follows the card that outputs the data on which you want to base a value to store.
-
Add a Set Variable card.
-
Specify a name for the variable. Names can include only numbers, characters, and underscores.
-
Specify a value for the variable.
-
If you want to specify a string value, ensure that the f(x) button appears to the right of the Value field. The presence of this button means that the field is in string mode, not function mode. You can type directly into the Value field and do not need to use quotation marks, unless you are specifying a string within a string.
Figure 5. The Value field in string mode, highlighted in the Set Variable card -
If you want to specify a DataWeave expression, ensure that
f(x)
appears in the right side of the Value field. The presence off(x)
means that the field is in expression mode. Clicking in the field opens the Custom Expression dialog, where you can compose an expression with the help of the Add data and Add functions panes on the right.Figure 6. The Value field in expression mode, highlighted in the Set Variable cardFigure 7. The Custom Expression window and its panesFor the Add data pane to appear, at least one of the previous cards must output a data type that is other than
Default
.For documentation about the DataWeave functions that are listed in the Add functions pane, see "Core" in the DataWeave Function Reference.
Example based on the screenshot of the Custom Expression window: If you wanted to save the payload’s error message into a variable, you would click
error_message
. The Expression field would then contain the expressionerror_message
. You would then click OK.Or you could also choose to type in a value. For example, if you wanted to store the entire payload in a variable, you could type
payload
in the Custom Expression field.
-
-
Click OK in the Custom Expression dialog and then click the canvas to close the card.
Examples
To store the payload from the previous card, set the value of the variable to payload
.
To store the attributes from the previous card, set the value of the variable to attributes
.
To store the entire set of data at the current point in a flow, set the value of the variable to message
.
To store a Boolean value, set the value of the variable to true
or false
.