If you’re developing software in Mendix, you’ll be inevitably using lists in your microflows. If you have a list, but want to use a single object from that list in another microflow, it can be tricky to get that object, because currently, Mendix has no list-indexing microflow-action.
In this post, I will show how you can use a Java action to get an object from a list, using a single line of code, which will work for any object type. And I will also give a solution that doesn’t require any Java-coding and just uses microflows.
Semi-generic Java action
You can use a Java action to get an element from a list. Unfortunately, it is not possible to have a generic Java action: it always needs to be type-specific. This example Java action takes a list of Customer-objects, and returns the index-th object within that list as a Customer object.
The user code for this Java action is not type-specific, which means you can use it in any such Java action (as long as you use the exact same parameter names and types). That is why I call this solution ‘semi-generic’. The single line of code is:
This will index the list 0-based, which means that the first object in the list has index 0 (this is standard Java behavior). To prevent out-of-bounds exceptions it is safer to add a check to the code. This way, the Java action will return empty if there is no object found at the index (e.g. when the list is empty):
return (index<list.size() && index>=0) ? list.get(index.intValue()).getMendixObject() : null;
The following microflow uses the Java action to get the first Customer object from the given list, and – if found – shows the edit-form for that customer:
Look, no Java!
Instead of using a Java action, you can also use a Microflow to get the first object from a list. The trick is to save the value of one attribute of the object to a local variable, and then using Find to get the object:
Details of this microflow:
- Create String variable $Name with value: empty
- Loop over $CustomerList:
- Change variable $Name to the value of the object’s Name-attribute. After the first iteration, Break. (removing the break event will get you the last element from the list)
- List operation: Find in CustomerList the first object of which the Name-attributes is equal to $Name. This object is $FirstCustomer (it will be empty if the list is empty)
- Return $FirstCustomer.
Which method is better? Depends on personal taste. Personally, I think the Java-variant is neater, because it does not depend on any specific attributes, so it is more “generic”. The weakness that ofter applies Java actions – renaming stuff in your domain model causes Java-actions to break – does not apply to this Java action, as the user code does not contain any application-specific types.
Other developers would say that you should not call Java during development in Mendix, (unless there really is no way to do what you want to do in a microflow), because Java code is harder to maintain and understand for other developers.
My experience is that a lot of times, you don’t even need to get 1 object from a list: just return the a list of 1 object and let the calling microflow deal with the list. However, in those cases where you really need 1 object, the above methods will come in handy. Which method do you prefer? Leave a comment below.