String to Enum: Java snippet for quick conversion

Have you ever had to deal with converting a simple input string into an enumeration (hereafter: string to enum) equivalent? More specifically: conversion of a string that is the equivalent of an enum’s key value (not to be confused with its caption). Chances are that if you have, the approach when only using Mendix would look like the one below:

Example of a string to enum with if then else statements
Example of a string to enum with if then else statements

Or even better, you created a sub-microflow to handle all of the possible outcomes like in the following microflow:

String to enum: especially useful in published webservices

While there’s nothing wrong in the above two approaches – in fact, everything is in your application’s model, so anyone can follow what’s going on – there are times where creating such solutions can be time-consuming and a simple pain. This holds especially true when you’re consuming a webservice, where string values need to be mapped to an enum in your domain model. Though this mapping is facilitated to some extent through the possibility of using “Convert with” microflows in your XML-to-domain mapping , it becomes a tedious task to do when having to handle multiple attributes that need to be converted into enum values which can consist of a large amount of keys.

For those wondering how to get to the “Convert with” options in your XML-to-domain mapping, simply open up your XML-to-domain mapping and double-click one of the attributes you wish to assign a microflow to. In the popup that follows, you now have the “Select…” option (in the last but one column) to assign your conversion microflow.

Example of the "Convert using" option for string to enum help in your XML-to-domain mapping
Example of the “Convert using” option for string to enum help in your XML-to-domain mapping

Speeding up the process with Java

So at one point in time I got to work with a colleague of mine and we encountered having to use the “Convert using” method in our XML-to-domain mapping as mentioned above. While you still have to create the microflows for each specific attribute of type enum, he showed me a neat little trick that can save you quite some time! Instead of using if then else statements for all possible values, here’s a little Java snippet that will simply take a string input and tries to match it to an enum of your choice.

if(input != null && !input.isEmpty() && Enum_MarketingCampaign.valueOf(input) != null) {
 return Enum_MarketingCampaign.valueOf(input).name();
 return null;

When using the above snippet, we assume that we have a string type input parameter, which will result in one of the values listed in the enum Enum_MarketingCampaign.

The only catch to this method is that you will be required to create Java actions for each enum you wish to target, as each Java action can only return one specific enum at a time. But, it can still save you a lot of time and hassle!

Now easier in Mendix 6.1.0 and above

While the above little snippet can still be useful in many situations, fortunately with the release of Mendix 6.1.0 the need to use these tricks to simplify processing of string values into enum equivalents is no longer needed, thanks to some mapping document improvements that were included. Among the improvements they made was the fact that the platform will now handle all string to enum conversions automatically, without you having to do a single thing!

“Previously, if you mapped between XML attributes of type string and Mendix enumerations you would need such a microflow. Because you would need to do this for every enumeration you want to map this could force you to make a large amount of these microflows. This has been resolved and XML attributes of type string now can be mapped to Mendix enumerations directly.”


Leave a Reply

Your email address will not be published. Required fields are marked *