External Functions Available In DataWeave

In addition to the built-in DataWeave functions, Mule Runtime and some modules, connectors, and components for Mule apps provide functions that you can use in DataWeave scripts. The functions are specific to the module, connector, and components.

Mule Runtime Functions

These functions are always available since they are injected by the Mule Runtime.

Accessing Properties (p Function)

The p function provides access to properties, whether these are:

  • Mule property placeholders

  • System properties

  • Environment properties

You can read more on configuring properties and how they are handled here.

The following example will log the value of the property

Example: Property Function Usage

<flow name="simple">
  <logger message="#[p('')]"/>

Executing a Flow (lookup Function)

The lookup function allows for executing a flow within your app and retrieves the resulting payload, taking the flow’s name and an input payload as parameters.

The specified flow is executed using the current attributes, variables, and error. It only replaces the payload for the first parameter, which must be a Java type.

Learn more about lookup here.

Matching Errors by Types (causedBy Function)

The causedBy function matches an error by its type, like an error handler does. This is useful when matching by a super type is required but specific sub-type logic also needed or when handling a COMPOSITE_ROUTING error that contains child errors of different types.

The error to match against can be implicit, but the type is always a required parameter.

In the following example, a global error handler is set up to handle SECURITY errors in a general way, while specific actions are set up for HTTP:UNAUTHORIZED and HTTP:FORBIDDEN errors.

Example: Error Matcher Function Usage

<error-handler name="securityHandler">
  <on-error-continue type="SECURITY">
    <!-- general error handling for all SECURITY errors -->
      <when expression="#[error causedBy 'HTTP:UNAUTHORIZED']">
        <!-- specific error handling only for HTTP:UNAUTHORIZED errors -->
      <when expression="#[causedBy('HTTP:FORBIDDEN')]">
        <!-- specific error handling only for HTTP:FORBIDDEN errors -->

Notice that the error parameter is used both explicitly and implicitly.

Connector and Component Functions

When using connectors and components in a Mule app, you can inject functions. Unlike the Runtime functions, these functions require a namespace, which usually matches the component name.

For example, in an app using Batch you can use the following expression: #[Batch::isSuccessfulRecord()].

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.