Microflow for checking if something already exists
Hi, I am developing a small online store in Mendix. I have a page called Categories, where I can enter new categories. I also have a button called “New”, that opens a pop-up for adding a new item in the Categories list and a “Save” button in this pop-up, but I want to have a condition for this Save button: when entering a new category, if that category already exists, I want to get a message that warns me that this category already exists. If everything is fine, I want to have the category added to the list. How should look the microflow (especially the decision activity)? Thank you! I will attach here screenshots with the domain model, the Categories page and the pop-up for adding a new category.
When you click on the save button, run it through a validation microflow to check if the category already exists in db.
Benjamin Luis Lim
You can use a database retrieve to check if the category with the same name already exists.
So add a retrieve from database, check first, set as xpath something like [Name = $Category/Name]
Lets call the return value Category_Existing
If this retrieve returns empty, you know it does not exist yet. If it is not empty, it exists already.
The decision would look like this: $Category_Existing = empty
The true-flow of the decision will continue to commit the new category, while the false flow will show a message to the user.
I think you will want to follow a standard Validation microflow set-up
When saving call a validation subflow
You give the new category as a parameter
Create a variable ‘valid’ (boolean)
Retrieve categories from the database, with xPath: Name = $newCategory/name
Add a decision: $retrievedCategory = empty.
If true, return $valid
If false, Set $valid to false. Show validation feedback on name and merge back to the mainflow (With valid being false)
In your save microflow: Check if $valid = true, if so commit the object and close. If false go to the end event without further actions. The user will see the validation feedback you made earlier.