All views and opinions are personal opinions of the Hosk

What are business rules

Business rules were added to CRM 2013 and a way to provide client side scripting/validating/field or section hiding without having to write any JavaScript. For context server side customization’s are plugins/workflows (e.g. code written in C# is executed on the server)

Business rules are also known as portable business logic (although I don’t know anyone who calls them that) because they also work on the mobile app.

What can business rules do

Set field values

Show/hide fields – Visibility

Enable/disable fields

change the requirement levels on fields (e.g. business required, recommend)

Show error messages

All the features above were usually done using Javascript in CRM 2011.

Why are business rules useful

Business rules are useful because they allow non developers to provide the functionality mentioned above on forms. Business rules can be used on Main and Quick Create Forms.

Are there any Business Rules Gotcha’s

You bet there are, here are the main ones

Fields updated using business rules do not trigger the fields on change event!

Business rules run only when the form loads and when field values change. They do not run when a record is saved.

Business rules only work with fields on the form (and the first 75 for tablets)

Business rules run only when the form loads and when field values change. They do not run when a record is saved.

Business rules are run in order of activation

Business rules only work client side, so won’t be triggered when data is changed server side (plugins, workflows, import)

Logic in business rules is applied. When there are multiple business rules, they are applied in the order they were activated, from oldest to newest.

There is also a big logical error which can be added using business rules and this is when you have either

Conflicting business rules

JavaScript and Business rules conflicting

Business rules will now mean there is an extra area to check when things are working in an usual many. Entities could have Javascript, multiple business rules, Workflows and plugins all updating the same fields. The possibilities are endless and so are the potential bugs.

As a general rule I would advise people not to mix JavaScript and business rules because it will make the solution more complex for developers to understand and maintain. Developers will also need to understand JavaScript will run first and then business rules (if the condition is true)

How do business rules work

Business rules are created on an entity basis

Business rules come in two parts, the condition and the action.

The condition is the criteria for the business rule to test to see if it runs. Currently business rules can have more than one condition and they work on an AND basis (e.g. all conditions have to be true).

If the condition is successful, then the action will execute.

You can view business rules a bit like real time workflows, but the actions can show/hide, enable/disable fields and show error messages etc.

Business rules also run only on the client side (e.g CRM FORM), which means they can only be triggered when adding/editing data on the CRM form.

Schoolboy error

Business rules usually have to be created in pairs and most people when they first use business rules they only create one.

You don’t need two but you usually do. If you hide a field/section with a business rule then you need another business rule to show the field/section otherwise it’s always hidden

Running Order

You could have a lot of things running on a form such as JavaScript and numerous business rules so you need to understand in what order things will run. It’s possible you could have JavaScript and numerous business rules all running against one field, so the order things run can have a dramatic effect on the outcome.

The logic included in your business rules is applied together with other logic in the form that could include system scripts, custom scripts, and other business rules. The order in which this logic is applied will affect the outcome. The order is as follows:

Any system scripts are applied first.

Any logic in custom form scripts is applied.

Logic in business rules is applied.When there are multiple business rules, they are applied in the order they were activated, from oldest to newest.

This means that to control the order in which business rules are applied, you must deactivate and reactivate the ones you want to be applied last.

I guess there isn’t any real way of knowing what Systems scripts are running or what they are doing so I will ignore those.

Javascript will run first

Business rules are run in order of activation. This sounds like a painful process of having to deactivate business rules and activate them in the order you want them to run (surely there must be an easier way), I can see some very tricky bugs to find

Business rule Scope

A bit like workflows, business rules have a scope but business rules are only concerned with forms. The scope choices are

All Forms

Choose one of the main forms

If you choose all forms, the business rule will run on the main form and Quick Create form but you cannot individually choose a Quick Create form.

Interesting thing to understand

Business rules get converted in JavaScript by CRM and then applied to the form. Business rules only work client side (not server side like workflows and plugins). The downside to this is business rules only get triggered on the form and not if the data is updated by any other means (bulk update, plugins, etc)

CRM 2015 – Business Rules enhanced

Business rules are going to be upgraded in CRM 2015 and I have seen it nicely put as Business rules enhanced

IF, THEN and ELSE Conditions

The biggest enhancement to business rules will be the adding of if statements. At the moment conditions must all equal true, this means you have to create two business for most functionality (e.g. one business rule to show a field and another business rule to hide a field)

And/or support

Conditions in business rules CRM 2015 will all combinations of AND or OR, with the limitation of only using them in one/single condition, so it’s a bit better.

Set Default Value

A business rule to set default values for fields

Server side

Business rules will be able to work server side. The reason this is important is because it means business rules won’t only work when the entity and fields are updated using the CRM form but also when bulk updates, imports or plugins changes those fields.

What hasn’t been fixed in CRM 2015

CRM 2015 business rules will be enhanced but they won’t be totally awesome yet, there will be a few errors which could still do with some improvement.

Complex conditions

Conditions have been improved but they are limited to one If/Else in a condition.

Hide/Show Sections and Tabs

I don’t think you can hide/show sections and tabs in the CRM 2015 enhanced business rules.

Cannot clear a field

You can set a default but you cannot null or clear a field using business rules

Related entity fields

One of the great things about work flows is you can update related entities specified in a lookup field, this would be great in business rules and save people do this using OData calls in Javascript.

Formula

Formula’s could be enhanced. E.g. dynamic dates can only be created by adding on days (not hours)

A very interesting article. Thanks. I have an issue with business rules and that is I have found that every now and then, they stop working. After much testing and investigation, I found that by deactivating all the rules on a form, then re-activating them, they started to work again. If you any view as to what causes this, I would be interested to hear it.

I haven’t heard of Business rules stop working totally. I would try raising this question on the forum where other people might have had some experience with this and your solution might help some other people.

I would perhaps consider raising a support ticket with microsoft when the business rules stopped working and they might be able to get some diagnostics.