Contact Free trial Login

Scripting Module

The Scripting module executes custom logic written in a scripting language. In some cases, you might need to create custom code to perform all or part of a complex task, or to reuse modules that you have already written. Supported scripting languages:

  • Groovy

  • Python

  • Javascript

  • Ruby

Installation

The Scripting module is included in Anypoint Studio. If you do not see this module in the Mule Palette, add it using Add Module. Alternatively, you can search in Exchange for the latest version of this module, and add this module to your project dependencies.

Basic Usage

To configure a Scripting operation in Studio:

  1. Click the Scripting module from the Mule Palette.

  2. Drag the Execute operation of the Scripting module into your flow, for example:

    Scripting Execute component
  3. Double-click the Execute element to open the Execute configuration tab.

  4. From the General tab, provide your code in the Code text window, as shown in the next example.

  5. Choose the Engine for the execute component:

    • Groovy

    • Nashorn (JavaScript)

    • Jython (Python)

    • JRuby (Ruby)

Script with code

In the Configuration XML editor, the XML looks something like this:

Example: Basic Usage
<scripting:execute engine="jython" doc:name="Script">
    <scripting:code>
      def factorial(n):
          if n == 0: return 1
	  return n * factorial(n-1)

      result = factorial(10)
    </scripting:code>
</scripting:execute>

Note that you can also load code from an external file by using a File configuration property, for example:

Script with a file configuration property

In the Configuration XML editor, the XML looks something like this:

Example: Using a file configuration property to set a script’s code
<scripting:execute engine="jython" doc:name="Script">
    <scripting:code >${file::script.py}</scripting:code>
</scripting:execute>

Setting Parameters

You can define input parameter values for the script to use through DataWeave, for example:

Script using parameters

In the Configuration XML editor, the XML looks something like this:

Example: Setting Parameters
<scripting:execute engine="jython" doc:name="Script">
	<scripting:code >def factorial(n):
	if n == 0: return 1
	return n * factorial(n-1)
result = factorial(initialValue + int(payload))</scripting:code>
	<scripting:parameters ><![CDATA[#[{initialValue: 10}]]]></scripting:parameters>
</scripting:execute>

Advanced Configuration

You can use the Advanced tab to modify the target value and to set a variable as the target of the scripting execution.

Advanced settings

In the Configuration XML editor, the XML looks something like this:

Example: Advanced Settings
<scripting:execute engine="jython" doc:name="Script" target="variableName">
    <scripting:code >${file::script.py}</scripting:code>
</scripting:execute

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub