Monday, 14 October 2013

Dynamics CRM 2013 - Business Rules

In Dynamics CRM 2013 a new feature that has been added is the Business Rules engine. This has two clear strengths compared to using JavaScript.

  1. Allows for "Non Developers" to create them due to the simple to use interface.
  2. Business Rules unlike JavaScript work with mobile forms.
There are going to be cases where the business rules won't suit and JavaScript will be required however in most cases a rule that is similar to JavaScript should be possible for minor functions.

For example:
On Address forms we wanted to default the country. The code looked like this

// If its a create form

if (Xrm.Page.ui.getFormType() == 1)
{
    Xrm.Page.ui.controls.get("country").getAttribute().setValue("My Country");
}

Business Rules do not appear to allow you to state what the form mode is. Which does reduce some of the logic that can be used. Below is a screenshot of a Condition.

Business Rules - Condition
A few things to notice about the above form. Firstly you can define the scope for your business rules in case you only want it to work on certain forms. Secondly the structure of a Condition is quite similar to the Advanced Find criteria. This means that staff who have experience with Advanced Find's can now write business rules. 

For my example I want to set the country attribute. The JavaScript was essentially defaulting the value so to achieve this I will say. Where country does not contain data as shown below:

Business Rules - Country Condition

Then I can then select an action from the Action menu shown below:

Business Rules - Action Menu

In this case we want to set the country value to "My Country". Which uses the "Set field value" option.

Business Rules - Set field value
Once I have created my action I can enter my description of my rule and my rule name. Save and close the Business Rule and Activate it.

When you go to the form you will see that My Country will magically appear. If you blank the field then move to another field it will appear again.

Business Rules - Rule in action
Is the above solution perfect? No. If a client would like to leave the field blank then they won't be able to. Ideally we would be able to (as the JavaScript does) only do this on form load or only set it when a user has populated the address. NOTE: If Street 1 is mandatory as above I recommend adding a second condition to only populate when Street 1 contains data.

As of the current release its clear to see the benefits of the Business Rules and the limitations. However its a fantastic starting point long time users of CRM will agree!

A few things of note:
  • Conditions appear to currently be AND only. It is not possible to group or OR Conditions.
  • As stated above Business Rules are currently based only on a field. You cannot state "On Save", "On Create", "On Load" etc.
  • Something from my personal wish list is filtering an Option Set based on criteria. However this might be a fair way into the future!

2 comments:

  1. Hello, this was a great past and it enabled me to calculate an estimated revenue on our opportunity form (probability*estimated revenue)/100 BUT it does not save in the field, it just simply refreshes when I open the form. How can I get it to save the value? thanks!

    ReplyDelete
  2. If you are using the "Set field value" option then it should be setting the value of the field. When the form is saved then the value should be set. As I said at the end of the post unfortunately you can't do "On Save" or anything so the Business Rule will still run every time the form is loaded.

    ReplyDelete