The new way to call Mendix microflows from Java actions

Robert Price
Mar 3 · 3 min read

Mendix 8.7 has just been released, and with it comes a new way to call microflows in Java actions.

A new set of fluent Java actions have been added to replace the old execute method in the Core. We now use microflowCall to call a microflow, along with associated inTransaction, withParam, withParams, and execute methods. These form part of the new MicroflowCallBuilder interface.

Let’s have a look at a couple of practical examples.

First, we need to build a microflow for our Java Action to call. In this case, the microflow will accept two parameters, a String called Name, and an Integer/Long called MessageCount. There is a single activity in the microflow that just says “Hello Name, you have MessageCount messages.”. Name and MessageCount are substituted in from the microflow parameters. We’ll save it SUB_ShowMessageCount.

Our first Java action will use the withParam option when calling the microflow. First, we create the Java action. When we “Deploy for Eclipse” this creates a stub Java action for us to populate. In this case, we want to tell Donald he has 1234 messages.

// BEGIN USER CODEcom.mendix.core.Core.microflowCall("TestCallingJava.SUB_ShowMessageCount").inTransaction(true).withParam("Name", "Donald").withParam("MessageCount", "1234").execute(this.getContext());return null;// END USER CODE

If we call this Java action we call the SUB_ShowMessageCount microflow in the TestCallingJava module. We say we want it in a transaction, we also pass the Name and MessageCount parameters and values, then finally execute it in the current context. This will give us the following message.

Donald sees this message.

If we don’t want to use multiple withParam methods, we can build up a Java Map of key values and pass this using the withParams method. To call the same example, but to tell Boris he has 3 messages, we can use the following Java code.

// BEGIN USER CODEjava.util.Map<String, Object> params = new java.util.HashMap<>();params.put("Name", "Boris");params.put("MessageCount", 3);com.mendix.core.Core.microflowCall("TestCallingJava.SUB_ShowMessageCount").inTransaction(true).withParams(params).execute(this.getContext());return null;// END USER CODE

This looks very similar to our previous example, but at the start, we build a HashMap of key values to pass in using withParams. Running this we get the following message on the screen.

Boris sees this message.

In summary

I hope this has given a useful overview on calling a Mendix microflow from a Java action. The changes Mendix have made here feel easier and clearer, which should lead to better code maintainability. Now… Go Make It!

Mendix Community

The community-sourced publication for low-code

Robert Price

Written by

With over 20 years of industry experience, Rob is primarily focused on delivering low code solutions using the Mendix platform. Find him at robertprice.co.uk

Mendix Community

The community-sourced publication for low-code

More From Medium

More from Mendix Community

More from Mendix Community

BFS: From Alice to Mendix

More from Mendix Community

More from Mendix Community

Riding the Nashorn in Mendix

More on Mendix from Mendix Community

More on Mendix from Mendix Community

Unittesting with Mendix

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade