Return Enum in java action

2
Hi all, I have created a java action and I've set the return type to enum. When I edit this action I see that executeAction() returns a string, but I expected it to return an enum. I have tried to import the enum with 'import modulename.proxies.Enum_Colors;' and than change the method return type for executeAction() to Enum_Colors, but this doesn't seem to work (error: The return type is incompatible with UserAction<String>.executeAction()). Does anyone know how to return an enum value? Thanks!
asked
2 answers
3

Hi,

Using a string as Rob suggests will work just fine but it has a major drawback IOP.
If the enum definition is changed or removed then you will not notice this until you run your java action.
An easy way around this is to use the proxies that mendix generates for you.

For example:

system.proxies.DeviceType.Phone.name();

Hope this helps,

Andrej

answered
2

Hi Martin,

  As you discovered, an enumeration is just a string.  The Mendix platform will convert it to the right enumeration for you so it is much easier than you think.  For example, if I have a java action that returns the Enumeration System.DeviceType enumeration, all I need to do is have my Java action return the string "Phone" and the Mendix platform will map that to the enumeration value automatically.  I haven't done a complete set of tests to see if the matching is case sensitive or if it will map to the Name as well as Caption, but just return the enumeration value you want as a string.  In my example, the following Java code is enough for my Java action to return, in a microflow, the DeviceType.Phone enumeration value.

public class getDeviceType extends CustomJavaAction<java.lang.String>
{
	public getDeviceType(IContext context)
	{
		super(context);
	}

	@Override
	public java.lang.String executeAction() throws Exception
	{
		// BEGIN USER CODE
		return "Phone";
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@Override
	public java.lang.String toString()
	{
		return "getDeviceType";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

 

answered