Mendix Rules OK!
Rules are a part of Mendix that are often overlooked.
In Mendix, Rules are a special kind of Microflow that can only return a Boolean or an Enumeration without side effects.
So, what are the differences between a Rule and a Microflow? This is what the Mendix documentation says.
The differences between a Rule and Microflow.
- A rule can only be used in a decision
- The return type has to be a Boolean or an Enumeration
- A rule cannot change data in the database; the actions to create, delete, change, and rollback objects are not available in rules
- A rule cannot interact with the client; the actions to show or close forms, show messages, send validation feedback, and download files are not available in rules
- A rule cannot call web services, generate documents, or import XML
Why use a Rule?
As a Rule is so limited compared to a Microflow, why would we want to use one? Well, because of the limitations, they actually perform better than a Microflow.
As they don’t have side effects, they are also very easy to write Unit Tests for. However, for me, the most important use is reusability and clarity.
How to use a Rule?
Rules are used in a Microflow Decision. We select the Rule radio button in the Decision, and then select the Rule we want to use, and set any parameters.
If you have the Community Commons module installed in your application, you have two Rules ready to be used. These are IsEmptyString and IsNotStringEmpty.
These take a String parameter and return a Boolean indicating whether the String is empty.
This is how we’d use it in a microflow.
In this example, I am checking if the Name attribute in my Person object is not empty.
If it is empty, I set a Boolean variable to False and show some Validation Feedback to the user. These are actions I can’t do from the Rule itself.
Inside the IsNotEmptyString Rule
Let’s have a look inside the Community Commons IsNotEmptyString Rule to see how it works.
As you can see, it’s very lightweight. The logic is all held in the End event.
$String != empty and length(trim($String)) > 0It first tests if the String is not empty, and then checks that the trimmed String is not of zero length. Trimming the String removes any whitespace from the start and end of the String. This returns a Boolean, which is also the return value from the Rule.
Writing our own Rule
If an existing Rule doesn’t meet your requirements, it is very easy to write your own.
Get Robert Price’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s say we want to check if a Date of Birth is valid. What would we want to do?
Well, firstly, we’d want to check that the Date of Birth was not empty. Secondly, we’d probably want to check that the Date of Birth was in the past, as a future date would mean they hadn’t been born yet. Let’s put this into a Rule.
In Studio Pro, go into your Application, select Add Other, and select Rule.
I name all my Rules with RULE_, so in this case, I’m going to call it RULE_DateIsInThePast.
Opening up my Rule, I’m going to add a DateTime Parameter called Date.
In my End event, I’m going to add the following logic.
$Date != empty
and
$Date < [%CurrentDateTime%]So my finished Rule should look like this…
To use my new Rule to test a Date of Birth in my Person object, I can add the following to a Microflow.
Using Rules in a validation Microflow
Let’s put our Rules to good use and use them to validate that a Person object is valid before committing.
In this case, we want to check that a Person object’s Name attribute is not empty, and that the Date of Birth attribute has a date that is in the past.
The microflow will be run from a Save button on a page that has allowed a Person to be created or edited.
Firstly, we create a Boolean called isValid. This is set to True. We then call our Rules as we explained earlier. If isValid is still True at the end, we commit the Person object and close the page. If it’s False, we drop out. This will show the Validation Feedback on the page so the user knows to correct their input.
Conclusion
I hope that this has helped explain how Rules work in Mendix and that you are inspired to use them more in your own projects.
Read more
From the Publisher -
Inspired by this article to bring your ideas to life with Mendix? Sign up for a free account! You’ll get instant access to the Mendix Academy, where you can start building your skills.
For more articles like this one, visit our Medium page. And you can find a wealth of instructional videos on our community YouTube page.
Speaking of our community, join us in our Slack community channel. We’d love to hear your ideas and insights!










