Contact Us 1-800-596-4880

Dynamic Evaluate (<ee:dynamic-evaluate/>)

Evaluates an expression to select a DataWeave script, and then executes the new script to generate a result.

The script can use any of the usual predefined variables, such as message, payload,vars, or attributes, but you can also add custom ones by providing a set of key-value pairs.

Component XML

This component supports the following XML structure:

<ee:dynamic-evaluate
  expression=""
  doc:name="Dynamic evaluate"
  doc:id="ojfqoz">
  <ee:parameters>#[]</ee:parameters>
</ee:dynamic-evaluate>

Dynamic Evaluate (<ee:dynamic-evaluate/>) attributes are configurable through the UI and XML.

Attribute Name Attribute XML Description

Dynamic evaluate (default)

doc:name

Editable name for the component to display in the canvas.

N/A

doc:id

Automatically generated identifier for the component.

Expression

expression

Expression that selects a DataWeave script that Mule executes, for example, expression="#[vars.generateOrderScript]".

Additional Bindings

parameters

N/A

Target Variable

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. See Enrich Data with Target Variables in the Mule documentation.

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. See Enrich Data with Target Variables in the Mule documentation.

<ee:parameters/> is a child element of Dynamic Evaluate.

Child Element Description

<ee:parameters/>

Key-value pairs that serve as parameters that the DataWeave script can evaluate, for example, <ee:parameters>#[{name: attributes.queryParams.userName}]</ee:parameters>.

Example

The following example selects a script from a database through a userId query parameter and stores that script in a userScript variable. Dynamic Evaluate (<ee:dynamic-evaluate/>) accesses the userScript variable to invoke the script by using the provided parameter name, which contains the value of attributes.queryParams.userName.

<flow name="dynamic-evaluate-example-flow">
  <http:listener config-ref="HTTP_Listener_Configuration" path="/"/>
  <!-- This SQL query uses queryParams.userId to dynamically select a DataWeave script stored in a Database,
  and then assign this script to target variable userScript-->
  <db:select config-ref="dbConfig" target="userScript">
    <db:sql>#["SELECT script FROM SCRIPTS WHERE ID = $(attributes.queryParams.userId)"]</db:sql>
  </db:select>
  <!-- The dynamic evaluate component executes the script stored in vars.userScript-->
  <ee:dynamic-evaluate expression="#[vars.userScript]">
    <!-- This line sets a parameter called 'name', so the expression in the Dynamic Evaluate component can use it -->
    <ee:parameters>#[{name: attributes.queryParams.userName}]</ee:parameters>
  </ee:dynamic-evaluate>
</flow>

Assume that the scripts lsalander and`mblomkvist` are stored in this example’s database:

Example: lsalander script
output application/json
---
{
  message: "Order " ++ attributes.queryParams.orderId ++ " has been received from " ++ name,
  items: payload.items
}
Example: mblomkvist script
output application/x-www-form-urlencoded
---
{
  message: "Order " ++ attributes.queryParams.orderId ++ " has been received from " ++ name,
  items: payload.items
}

Example Application Behavior

When Mule application example receives lsalander as the queryParams.userId in the request, Mule executes the lsalander script, which results in a JSON response. If the application receives mblomkvist as the queryParams.userId value, Mule executes a different script that generates a x-www-form-urlencoded response.

The example demonstrates how the response type can be parameterized based on the user, but the entire response can be parameterized to suit user needs.