CRM 2013 – Understanding Business Rules

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.

I have written a quick guide to business rules here

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 appliedWhen 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

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.

Business Rules 1

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

To read more about this, read the blog post below

CRM 2013 – Business Rules work in pairs because the condition is AND and not IF

 

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.

According to this MSDN article on business rules

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:

  1. Any system scripts are applied first.
  2. Any logic in custom form scripts is applied.
  3. 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)

Here is a good article on the new IF, THEN

http://www.magnetismsolutions.com.au/blog/paulnieuwelaar/2014/09/25/new-conditional-statements-microsoft-dynamics-crm-2015-business-rules

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)

 

 

Further reading for CRM 2013 Business rules

http://www.dynamics101.com/2014/07/understanding-business-rules-microsoft-dynamics-crm-2013/

http://blogs.technet.com/b/lystavlen/archive/2013/10/13/crm-2013-understanding-portable-business-logic.aspx

 http://blog.sonomapartners.com/2014/09/dynamics-crm-2015-first-look-enhanced-business-rules.html

http://msdn.microsoft.com/en-us/library/dn531086.aspx

http://www.powerobjects.com/blog/2013/10/24/become-pseudo-developer-business-rules-crm-2013/

http://msdynamicscrmblog.wordpress.com/2013/11/05/business-rules-in-dynamics-crm-2013/

http://www.c2software.com/c2-blog/business-rules-dynamics-crm-2013.aspx

 

18 thoughts on “CRM 2013 – Understanding Business Rules

    • Hosk October 23, 2014 / 8:22 am

      Hi

      Business rules run on the outlook client, in fact business rules run everywhere.
      Web Client – All Browsers
      Outlook client – Offline and online
      Microsoft Dynamics CRM for tablets (Surface and iPad)

      Like

  1. Chris November 21, 2014 / 8:22 am

    Hello

    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.

    many thanks

    Chris

    Like

    • Hosk November 21, 2014 / 8:48 am

      Hi Chris

      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.

      Like

      • Hosk November 24, 2014 / 11:40 pm

        Hi Chris

        Since you left this comment I have seen quite a few business rule posts on the forum with the same/similar problems you were experiencing. I believe Microsoft even managed to recreate the problem.

        I also think someone raised a request on connect.com.

        hopefully this will be fixed in CRM 2015 when they make business rules run server side and maybe do some tinkering with how business rules work to resolve this problem.

        Like

    • Hosk January 7, 2015 / 9:21 am

      Business rules should definitely have the functionality to allow you to order them. The current process of creation order is not very obvious or useful.

      I have given it my vote

      Like

  2. Alisha January 7, 2015 / 9:17 am

    Hi, Does Business rules work for Quick view form ?

    Like

    • Hosk January 7, 2015 / 9:19 am

      Yes.

      You can choose to apply business rules to individual forms or all forms (which includes Quick View Form)

      Like

  3. Jay February 25, 2017 / 3:58 am

    Have you ever seen a situation where you set up a business rule but certain fields which are part of the entity and on the form are not available in the Action drop down to set as Business Required?

    Like

    • Hosk February 27, 2017 / 7:55 am

      No

      Like

  4. Inês March 1, 2017 / 2:32 pm

    Hi Hosk, nice article! Thanks for sharing.

    So, I had a problem with the running order of business rules in my project.

    It was very hard publish and unpublish the BR’s to the correct order, so i’m thinking develop a kind of plugin to autommatically publish my BR’s on the correct order.

    Are you aware of the existence of something similar, that does it automatically?

    Best Regards,
    Inês.

    Like

    • Hosk March 1, 2017 / 4:13 pm

      I have not seen anything which will help automate this

      Like

Leave a comment

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