Select Page

The friendly people from Microsoft released the ability to manipulate certain properties in an application, based on the outcome of an easily definable rule set. This does not seem to be documented at the time, but this functionality can also be used for re-usable business logic on actions and other controls.

You first select the Control which the Rule should be tested against, before specifying the value to manipulate if the rule returns true. In the example below, there is a text input control on the left and two text labels on the right. Select the input control, and then click on the rules tab on the right to create rules associated with the input control. After clicking on new rule, you can specify the condition under which the rule should be effective (True). The below example will check if the TextInput3 control is blank before making the rule effective.

After specifying the Condition, you can define the action(s). Take note of the rule name at the top of the panel (Rule6), and another note that this can not be renamed….yet.

The system will now ‘record’ what changes you would like to make to certain controls in the event that the rule returns True. If you want to change the color of a text label for an example, select the control which should be manipulated if the rule is true, and simply change the color property. You will also notice the message at the top of the canvas saying you are busy ‘recording’ the actions.

The default color of the above text label is RGBA(47,41,43,1), but we would like to change this to red if the rule returns True. We can simply overwrite the existing value with the new value and PowerApps will automatically use the old value if the rule does not return true.

When we now click on done in the ‘recording’ message on the top of the canvas, PowerApps will be kind enough to automatically write the if statement to say that the color should be red if the rule returns true, while the old value should remain in the event that the rule returns false. This works well and we can see that the text color of Label15 immediately turns red.

If you type something in the text input, the text in Label15 turns black again.

All of this is very awesome, but what gets really exciting is the fact that this rule can be reused for other controls, or even certain checks during actions. We can now go and define more actions in the rule panel, or you can just type in “Rule6” where ever you want to test for a true or false. Even though the autofill does not pickup the rule names while typing, it will work at runtime. Please however note that the rule name is case sensitive. I can now type the following into the second text label on the right, which will make it adhere to the rule as well.

If(Rule6, Red, Green)

 

We can now also include the rule check in certain actions like buttons as well. The below button (even on another screen) will set a variable based on the outcome of the rule to test.

Manually adding the Rule to the button would have also automatically included the button action in the rule panel, so it is easy to see which controls are affected by the rule.

The above is a great way to centralize business rules and logic in the application. Instead of just updating colors of text, you can also use the above methods for some more advanced validations on actions which is greatly useful for workflow applications, without scattering formulas all over the application.

 

At the time of writing this blog rules could not be renamed, but luckily the PowerApps team released this functionality on the 10th of November 2017. Rules can now be named appropriately to accurately identify business rules. An example of this would be naming a rule “ExpenseApprovalDelegation” and having some advanced rules for the various roles who can approve expenses up to certain values, and easily using this logic in various properties and actions in order to adhere to these rules.