Contact Us 1-800-596-4880

Migrating the Scripting Component

Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule reached its End of Life on November 2, 2022, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

The <scripting> component in Mule 3 is replaced with a new <scripting> module. This new module is pretty similar in functionality, with just some syntax changes:

Mule 3 Example: Scripting
 <flow name="greedy">
    <choice>
        <when expression="#[flowVars['currency'] == 'USD']">
            <scripting:component>
                <scripting:script file="greedy.groovy">
                    <property key="currency" value="USD"/>
                </scripting:script>
            </scripting:component>
            </when>
        <when expression="#[flowVars['currency'] == 'GBP']">
            <scripting:component>
                <scripting:script file="greedy.py">
                    <property key="currency" value="GBP"/>
                </scripting:script>
            </scripting:component>
            </when>
    </choice>
</flow>

+

Mule 4 Example: Scripting
 <flow name="greedy">
    <scripting:execute engine="groovy">
        <scripting:code>${file::greedy.groovy}</scripting:code>
        <scripting:parameters>
            #[{currency: vars.currency}]
        </scripting:parameters>
    </scripting:execute>
</flow>

As you can see, the main difference here is that now you can use DataWeave to generate the input parameters.

Accessing variables from the script

The above example uses a variable to show how to pass parameters into the script. MuleSoft recommends that as a best practice to keep the script decoupled from the rest of the flow, so that changes to the Mule applications don’t require changes to the script.

However, you can still access the variables from the script using the vars keyword. For example:

Mule 4 Example: Accessing variables directly
<flow name="greedy">
    <scripting:execute engine="groovy">
        <scripting:code>return "currency is " + vars.currency</scripting:code>
    </scripting:execute>
</flow>

To use the Scripting module, simply add it to your application using the Studio palette or add the following dependency in your pom.xml file:

<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-scripting-module</artifactId>
  <version>1.1.0</version> <!-- or newer -->
  <classifier>mule-plugin</classifier>
</dependency>