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
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, } ]
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, } ]
Default.DWL
%dw 2.0 output application/java --- { message: "Object not matched to select the data" }
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
Sample Project: dynamicevaluate sample application
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