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.
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.
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!