Business Rules

Business Rules in Model Driven Apps

When you develop model driven apps, business rules can be very useful.

Introduction to business rules

In this posts I will look at the business rules and how they work.

In my example I have already created an entity called Fruit in my Common Data Services. The entity has two fields, Name and Colour.

I only want the colour field to be available when the Name of the fruit is Apple.

So for an Orange, the Colour field will disappear. While for an Apple we want to show the Colour field. It is quite obvious which colour oranges are!

Business Rules in Model Driven Apps 1

And for Apple the field will appear as shown below in my Model Driven Apps

Business Rules in Model Driven Apps 2

If we had to do this in code this would probably look a bit like this. This code is also shown by the Business Rule editor. It is quite helpful to check when things go unexpectedly wrong!

IF
Name equals "Apple"
THEN
Show field Colour
ELSE
Hide field Colour

Within the Business rule editor however, the rule can be ‘coded’ like this:

Business Rules in Model Driven Apps 3

Ok, this is all easy.

Now I’m going to add some more logic. As grapes can also come in different colours I’m adding a second condition that will check if my fruit is a grape.

Business Rules in Model Driven Apps 4

As you will notice in the above screenshot, it is important to name each box properly. It would have been nice if the logic was somehow represented in each box as well. But while that isn’t there the descriptions become critical.

As you change the description make sure that you hit the apply button! It isn’t always visible, but it is there when you scroll down!

Business Rules in Model Driven Apps 5

As you make your changes to a business rule it is important to deactivate the rule first and after all the updates you will need to activate the rule again.

So our new logic is something like this

IF
Name equals "Apple"
THEN
Show field Colour
ELSE IF
Colour equals "Grape"
THEN
Show field Colour
ELSE
Hide field Colour

Resulting in the following business rule:

Business Rules in Model Driven Apps 6

These multiple conditions may then well work, but is can be wise to follow the location described below. Keeping the number of steps in the overall business rule down.

Then I tried like this where the condition checks if the Name field is set to Grape or Apple and then disable the colour field as required.

Business Rules in Model Driven Apps 7

This worked! The condition in the above is set as shown below:

Business Rules in Model Driven Apps 8

It is however possible to make Business Rules with multiple conditions work in Model Driven Apps. It could look like this:

Business Rules in Model Driven Apps

Other things we can do with Business Rules

So far we have seen the option to Show or hide fields in the forms. Other actions you can handle with business rules are:

  • Set field values
  • Clear field values
  • Set field requirement levels
  • Enable or disable fields
  • Validate data and show error messages
  • Create business recommendations based on business intelligence

Limits of Business Rules

You might want to add multiple conditions however you can only add more conditions to the negative part of the branch of parent conditions. Multiple conditions in the true branch are in general not really needed. You could simply add more and/or logic in the existing conditions.

Business Rules in Model Driven Apps 9

When do Business Rules fire?

This is actually a very important question and important to understand. When a form is loaded the business rules will be applied.

Additionally when data changes in your form the rules are run as well. Making it possible for a form to be dynamic as the user completes the data entry process.

3 thoughts on “Business Rules in Model Driven Apps

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: