How to work with Variables in Java actions — Mendix How-to

Published in
6 min readApr 30, 2021

--

One of Mendix’s greatest strengths is the ability to add custom code extensions using Java actions. Anything related to your applications logic, which isn’t possible with out of the box functionality, is often 100% achievable with Java.

Knowing where to start can be difficult for new users and the less experienced low coders out there. So, this blog is going to walk you through how to create and write Java actions as well as how to work with variables and objects inside them.

Prerequisites

Before we get started, you will need to have a few things installed on your machine :

-Mendix Studio Pro 8 or above (I’m using 8,12 in this example)

-The Eclipse IDE which you can download at www.Eclipse.org/downloads/

Creating the domain model

In this how-to, I will be manually creating Mendix objects inside of a Java action, based off of data provided in the action’s parameters. In order to do this we have to create the entity in the domain model which the Java action should return. In your domain model, create an entity called Order. Now add 3 attributes, one for username (String), one for address(String), and one for order total (Decimal).

https://bit.ly/MXW21
https://bit.ly/MXW21

Creating the Java action

To create the Java action, simply right click in the project explorer and select to create a new Java action, found underneath the “Add Other” category.

Name your action something relevant and be sure to follow naming conventions. The convention for Java actions is JA_(Your action Name). In my example I’m calling it JA_CreateOrder.

Double click the newly created Java action in the project explorer to open it. In the General Tab, add the same three parameters we added to the order entity. Make sure to distinguish the parameter names from the originals, I like to add the suffix “_Param” to my Java action parameters so it’s easy to tell them apart in the code.

Also set the return type of the Java action to be of type object, and then select the order entity we created earlier.

Working with Eclipse

While in Studio Pro, you can press F6 or go to Project -> Deploy for Eclipse.

Then in Eclipse go to File -> Open Projects from File System. In the window for input source choose directory and then select your project’s root folder. Click finish to complete.

In the Project Explorer on the left, open the Java source folder, and go to myfirstmodule.actions. Here you will find the all the Java actions contained in that module, including the one we just created.

What’s already there?

Before we start coding, let’s take a look at what’s been created for us. You will notice that there are some imports automatically created, as well as a public class named after our Java action then all the parameters we added and a constructor for that class. We then have a few methods and two begin and end user code blocks. It’s really important to remember that, besides the imports, anything not inside these user code blocks will be overwritten at run time, so make sure not to code anywhere except those 3 areas.

Time to code!

In order to create an entity from our domain model using Java, we need to import the proxy created by Mendix for that object. Under imports add -

import myfirstmodule.proxies.Order;

Then inside the first user code blocks we need to create a context object, with

IContext context = getContext();

:We can then create our order entity, and pass the context object into the constructor with:

Order newOrder = new Order(context);

We can then set the attribute on our new order with the set methods created for us in the proxy we imported. So I set the three attributes to be the parameters of the Java action.

All that’s left to do is return the order object, we can do this using the method getMendixObject() as shown below:

return newOrder.getMendixObject();

This is important when dealing with entities as return values, the getMendixObject() method is also provided in the proxy.

We can now call this action from any microflow in your app. I’ve added a breakpoint to my code, so you can now run your app and test it out.

If you run into any problems or have a compilation error, you can get some troubleshooting tips from the documentation page for debugging java actions in Eclipse.

Final thoughts

Now you’re able to create custom java actions and work with data and variables within them. You might not be an expert but you are well on your way to being a better Java developer with Mendix.

Don’t be afraid to play around and experiment, why not try adding more attributes to the order entity or even try working with file objects. There are endless possibilities with Java and Mendix so go make them!

Full Java action code:

// This file was generated by Mendix Studio Pro.//// WARNING: Only the following code will be retained when actions are regenerated:// - the import list// - the code between BEGIN USER CODE and END USER CODE// - the code between BEGIN EXTRA CODE and END EXTRA CODE// Other code you write will be lost the next time you deploy the project.// Special characters, e.g., é, ö, à , etc. are supported in comments.package myfirstmodule.actions;package myfirstmodule.actions;import com.Mendix.systemwideinterfaces.core.IContext;import com.Mendix.webui.CustomJavaAction;import com.Mendix.systemwideinterfaces.core.IMendixObject;import myfirstmodule.proxies.Order;public class JA_CreateOrder extends CustomJavaAction<IMendixObject>{private java.lang.String Username_Param;private java.lang.String Address_Param;private java.math.BigDecimal OrerTotal_Param;public JA_CreateOrder(IContext context, java.lang.String Username_Param, java.lang.String Address_Param, java.math.BigDecimal OrerTotal_Param){super(context);this.Username_Param = Username_Param;this.Address_Param = Address_Param;this.OrerTotal_Param = OrerTotal_Param;}@java.lang.Overridepublic IMendixObject executeAction() throws Exception{// BEGIN USER CODEIContext context = getContext();Order newOrder = new Order(context);newOrder.setUsername(Username_Param);newOrder.setAddress(Address_Param);newOrder.setOrderTotal(OrerTotal_Param);return newOrder.getMendixObject();// END USER CODE}/*** Returns a string representation of this action*/@java.lang.Overridepublic java.lang.String toString(){return "JA_CreateOrder";}// BEGIN EXTRA CODE// END EXTRA CODE}

Read more

From the Publisher -

If you enjoyed this article you can find more like it at our Medium page or at our own Community blog site.

For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.

Interested in getting more involved with our community? You can join us in our Slack community channel or for those who want to be more involved, look into joining one of our Meet ups.

--

--

Ryan Mocke
Mendix Community

London Based, Developer Evangelist. I create content for the Mendix developer community, to help them achieve success in their projects.