---
schema_version: "1"
id: 7dbf605e645efee6edcec85f8f02b9d54f533f89fbe5ffe7d126c7ba95703452
title: Dynamic Evaluate Component
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/4.9/dynamic-evaluate-component-reference.md
canonical_url: https://docs.mulesoft.com/mule-runtime/4.9/dynamic-evaluate-component-reference
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 7746c2e70207ff7ff5b7f9da51eafec83b4f29a078b3b952f6f2ca12c6bb16c4
description: MuleSoft Documentation Site
release: "4.9"
latest: false
---
---
product: Mule Runtime
version: 4.9
keywords: mule, ee, esb, studio, dynamic, evaluate, expression
---

# Dynamic Evaluate Component

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The Dynamic Evaluate component evaluates an expression to select a DataWeave script, and then
executes the new script to generate a result. This behavior enables you to dynamically select the
script, instead of hardcoding it into the [Transform Message component](https://docs.mulesoft.com/mule-runtime/4.9/transform-component-about).

The script can use any of the usual
[context variables](https://docs.mulesoft.com/dataweave/latest/dataweave-variables-context), such as `message`,
`payload`,`vars`, or `attributes`, but you can also add custom ones by providing a set of key-value
pairs.

## Dynamic Evaluate Configuration

<table><colgroup><col> <col> <col> <col></colgroup><thead><tr><th>Field</th><th>Value</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td><p>Expression</p></td><td><p>DataWeave expression</p></td><td><p>Specifies an expression that selects a DataWeave script that Mule then executes.</p></td><td><p><code>expression="#[vars.generateOrderScript]"</code></p></td></tr><tr><td><p>Parameters</p></td><td><p>DataWeave expression</p></td><td><p>Specifies key-value pairs to set as parameters that the DataWeave script can evaluate.</p></td><td><p><code>#[{joiner: ' and ', id: payload.user.id}]</code></p></td></tr></tbody></table>

## Evaluate DataWeave Script from the File System Example

The following example shows how the Dynamic Evaluate component selects and executes a DataWeave
script stored locally in the file system. The application’s flow behavior is as follows:

1.  An HTTP Listener source listens to a POST URL request that contains a `locale` query parameter
    and a payload.

2.  A File Read operation uses the `locale` query parameter received in the HTTP request to search
    locally the DataWeave script that matches the parameter name.

3.  The File Read operation stores the content of the script into a new variable.

4.  A Dynamic Evaluate component executes the DataWeave expression stored in the variable.

5.  A Logger component logs the payload result of the transformation.

The locally stored DataWeave scripts can be stored in a database instead of locally, and the app can
be modified to pick the files from the database instead of the file system. This enables you to
replace any of the `.dwl` files while the app is deployed and running, changing the DataWeave script
to execute without having to modify or redeploy the application.

In this example, you create two DataWeave scripts and store them locally, create the Mule
application, and run and test the application:

![A flow showing a dynamic evaluation component in Anypoint Studio](/mulesoft/current/en-us/mule-runtime/4.9/_images/dynamic-evaluate-flow-1.png)

Figure 1. Dynamic Evaluate Flow example in Anypoint Studio

### Create and Store the DataWeave Scripts

The first step in evaluating DataWeave scripts from the file system is to create and locally store
the scripts:

1.  Create a new Mule project in Studio.

2.  In the **Package Explorer** window, under the name of your Mule project, right-click the
    **src/main/resources** folder and select **New > File**.

3.  In the **File name** field, add the name `dw_en-US.dwl` to create the new DataWeave script file.

4.  Click **Finish**.

5.  In the **dw\_en-US.dwl** window, paste the following DataWeave expression:

    ```dataweave
    %dw 2.0
    output application/json
    ---
    {
        "First Name": payload.name,
        "Last Name": payload.last,
        "Country": payload.loc
    }
    ```

6.  Repeat the previous steps and create a new file named `dw_es-AR.dwl` that contains the following
    DataWeave expression:

    ```dataweave
    %dw 2.0
    output application/json
    ---
    {
        "Nombre": payload.name,
        "Apellido": payload.last,
        "País": payload.loc
    }
    ```

7.  Save your changes.

### Create the Mule Application

After creating and locally saving your example scripts, create the Mule application in Studio:

1.  In the **Package Explorer** window **src/main/mule (Flows)** folder, navigate to your flow’s
    `.xml` file.

2.  In the **Mule Palette** view, select the **HTTP Listener** source and drag it onto the canvas.  
    The source initiates the flow by listening for incoming HTTP requests.

3.  Set the **Path** field to `/test`.

4.  Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of the HTTP Listener in the app.

5.  On the **General** tab, configure the following fields:

    - **Host**: `All Interfaces [0.0.0.0] (default)`

    - **Port**: `8081`

6.  Click **OK**.

7.  Drag the **File Read** operation to the right of **HTTP Listener**.

8.  Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of the File Read operation in the app.

9.  On the **General** tab, set the **Working Directory** field to the main resources folder that
    contains your Studio project and previous DataWeave scripts:

    `/Users/username/AnypointStudio/studio-workspace/dynamicevaluate/src/main/resources`

10. Click **OK**.

11. In the **File path** field, set the path `#["dw_" ++ attributes.queryParams.locale ++ ".dwl"]`.

    The connector reads the locally stored DataWeave script file names that start with `dw_` and
    contains the `locale` query parameter (`attributes.queryParams.locale`) received in the HTTP
    request.

12. In the **Advanced** tab, set the **Target Variable** field to `dwScript`.  
    This variable stores the DataWeave script that matched the file search.

13. Drag the **Dynamic Evaluate** component to the right of **File Read**.

14. Set the **Expression** field to `#[vars."dwScript"]` to execute the matched DataWeave script
    saved in the variable `dwScript`.

15. Set the **Target value** field to `payload`.

16. Drag a **Logger** component to the right of **Dynamic Evaluate**.

17. Set the **Message** field to `#[payload]`.

18. Save your Mule application.

### Run and Test the Mule Application

After you create your Mule application, run and test it:

1.  Click the project name in **Package Explorer** and then click **Run > Run As > Mule
    Application**.

2.  Send a POST request to `http://localhost:8081/test`, appending to the URL any of the following
    query parameters `locale=en-US` or `locale=es-AR`, for example:  
    `http://localhost:8081/test?locale=en-US`

    Additionally in the POST request, send the following payload:

    ```JSON
    {
    "name": "Max",
    "last": "The Mule",
    "loc": "Argentina"
    }
    ```

3.  In the Studio Console, scroll through the logs to see the result message:

    ```console
    INFO  2021-01-19 11:18:25,559 [[MuleRuntime].uber.12: [dynamicevaluate].dynamicevaluateFlow.CPU_INTENSIVE @2588f400] [processor: dynamicevaluateFlow/processors/2; event: 319aacc0-5a61-11eb-bbb8-f01898ad2638] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: {
      "First Name": "Max",
      "Last Name": "The Mule",
      "Country": "Argentina"
    }
    ```

### XML for Evaluating DataWeave Script from a File System

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:file="http://www.mulesoft.org/schema/mule/file"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<file:config name="File_Config" doc:name="File Config" >
		<file:connection workingDir="/Users/ktroller/AnypointStudio/studio-workspace/dynamicevaluate/src/main/resources" />
	</file:config>
	<flow name="dynamicevaluateFlow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/test"/>
		<file:read doc:name="Read" config-ref="File_Config" path='#["dw_" ++ attributes.queryParams.locale ++ ".dwl"]' target="dwScript"/>
		<ee:dynamic-evaluate doc:name="Dynamic Evaluate" expression='#[vars."dwScript"]'/>
		<logger level="INFO" doc:name="Logger" message="#[payload]"/>
	</flow>
</mule>
```

## Evaluate DataWeave Script from a Database Example

The following example selects a script from a database through a `userId` query parameter and stores
that script in a `userScript` variable. The `dynamic-evaluate` component accesses the `userScript`
variable to invoke the script using the provided parameter `name`, which contains the value of
`attributes.queryParams.userName`.

```xml
<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>
```

Consider the following scripts stored in this example’s database for entries `lsalander` and
`mblomkvist`, respectively:

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 this example Mule application receives `lsalander` as the `queryParams.userId` in the request,
Mule executes the corresponding 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.

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