Dynamic Evaluate In Mule 4

 

In this tutorial we will demonstrate how can we Select Dataweave(DWL file)dynamically using Dynamic Evaluate In Mule 4.

Dynamic evaluate is useful if you wanted to select the dataweave script dynamically out of multiple DWL files on the basis of some conditions passed at runtime.

 

Create a sample project in anypoint studio and configure the listener to trigger the request

Select Dataweave(DWL file)Dynamically Using Dynamic Evaluate In Mule 4

 

Create 3 Different DWL’s file to select dynamically

Customer.DWL

%dw 2.0
output application/java
---
[
  {
    "FirstName": "Andy",
    "LastName": "Rose",
    "CustID":111,
    "CustAddressID":4566,
    "CustContactID":3454,
  },
  {
    "FirstName": "Grant",
    "LastName": "Flower",
    "CustID":678,
    "CustAddressID":4566,
    "CustContactID":3454,
  }
]

Select Dataweave(DWL file)Dynamically Using Dynamic Evaluate In Mule 4

 

Employee.DWL

%dw 2.0
output application/java
---
[
  {
    "FirstName": "John",
    "LastName": "Mike",
    "EmpID":123,
    "EmpAddressID":4566,
    "EmpContactID":3454,
  },
  {
    "FirstName": "Eves",
    "LastName": "Andrew",
    "EmpID":234,
    "EmpAddressID":4566,
    "EmpContactID":3454,
  }
]

Select Dataweave(DWL file)Dynamically Using Dynamic Evaluate In Mule 4

 

Default.DWL

%dw 2.0
output application/java 
---
{
  message: "Object not matched to select the data"
}

Select Dataweave(DWL file)Dynamically Using Dynamic Evaluate In Mule 4

 

Create a Dataweave Transformation to select the file on some condition

%dw 2.0
output application/java
---
if(attributes.queryParams.object =="customer")
    readUrl('classpath://dwl/customer.dwl', 'text/plain')
else if(attributes.queryParams.object =="employee")
    readUrl('classpath://dwl/employee.dwl', 'text/plain')
else
    readUrl('classpath://dwl/default.dwl', 'text/plain')

 

configure the dynamic evaluate, pass the expression as variable used to select the dwl file

 

Transform the payload

%dw 2.0
output application/json
---
payload

 

Deploy the application on local and test with different conditions

Test with employee as Query Parameter

 

Test with customer as Query Parameter

 

Test with queryparamter as employee1 which does not match any condition and default.dwl will be invoked

Select Dataweave(DWL file)Dynamically Using Dynamic Evaluate In Mule 4

 

Sample Project: dynamicevaluate sample application

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Susheel
Susheel
3 months ago

Hi, Can you also provide POC on Lookup function with in dataweave and provide scenarios to use lookup flows or functions with in dataweave. Best practise to use lookups and when not to use lookups within dataweave