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:

  1. Reusable
  2. Cleaner code
  3. Easy Maintenance
  4. 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/

Custome Function location

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 :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store