Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mule 4: Creating Global Custom Functions in Dataweave 2.0

DZone 's Guide to

Mule 4: Creating Global Custom Functions in Dataweave 2.0

In this article, I will explain how to create a global function that can be access or reuse by other Transform Message Component within a Mule Application.

· Integration Zone ·
Free Resource

In this article, I will explain how to create a global function that can be access or reuse by other Transform Message Component within a Mule Application.

First, We need to create a Dataweave File (.dwl) and save it in src/main/resources/modules/. Now,we can define our global custom functions here by using the fun directive and global variable by using var directive. Also note thatthis dwl file should not contain an output directory, body expression, or the separator (---) between header and body sections.

Image title

After creating a Global Dataweave Module, we can now reuse and import it into another Dataweave Script, any functions, variables, types, and namespaces defined in that custom module become available for use in the Dataweave Body.

Image title

We can use the import directive to import the custom module modules::MyGlobalCustomFunctions, which is stored in src/main/resources/modules/MyGlobalCustomFunctions.dwl into another Dataweave Script. On the above code, I used alies (as) to reference the instance of the imported custom module to a call alies, in this case, I can simply invoke the global function using call::formatDate instead of MyGlobalCustomFunctions::formatDate.

That's it, For more details about this new functionality of Dataweave 2.0, Check this oout: https://docs.mulesoft.com/mule4-user-guide/v/4.1/dataweave-create-module

Topics:
mulesoft ,mule 4 ,dataweave 2 ,function ,global ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}