Mulesoft — Global Function in Dataweave 2.0
This article is to create a global function that can be accessed or reused by other Transform Message Component within a Mule Application.
Benefits:
- Reusable
- Cleaner code
- Easy Maintenance
- Modularity
How to implement: Function 1: check minimum and the maximum length of a string Function 2: Remove preceding zeros from a string
Step 1: Create a data weave file in src/main/resources/modules/
Step 2: Define global custom functions by using the fun directive Sample Dataweave code for functions.dwl:
Note: The dwl file should not contain an output directory, body expression, or the separator ( — -) between header and body sections.
Step 3: Import directive to import the custom module modules::functions, which is stored in src/main/resources/modules/functions.dwl into another Dataweave Script
simply invoke the global function using func::<function name>
Sample Code: https://github.com/gopal-ji-singh/mule-global_function_Dataweave
Cheers :)