A common problem I come across in developing Mendix applications is when I have to manipulate an entity in a microflow which may or may not exist. This quantum occurance would occur when having to update an entity from a 1-1 relationship, or on a 1-* where the child entity (on the many relationship side of things) is also related to a secondary attribute, such as a day or version.
When dealing with this situation you have to try retrieve the entity using the relationship or Key Attribute(such as date), check if it exists, if it doesn’t create the entity with the new Key Attributes. This now leaves us with two entities which have to be manipulated seperately or reretrieve the entity into a common name.
Going with the first case lets look at what my first pass solution would look like on the following Module:
As can be followed from the above microflow there will be much duplication of logic, and complication of the microflow at quick inspection.
Note: When checking to see whether an entity I retrieved exists I like to create an exclusive split with the caption ‘Exists?’ and the formula ‘not($Entity=empty)’. This keeps the reading of the flow in the positive light rather than negative.
The solution I follow now as my standard practice in these circumstances is to create a microflow called Entity_GetCreate which will handle this retrieve or create logic for me and return the respective entity so that I dont need to concern myself with handling these cases seperately.
This GetCreate microflow for the above entity would look as follows:
Which results in the original microflow being simplified to:
Much easier to read for business and mendix users.