How to use Business Rules to determine Form Type in Dynamics CRM

Introduction

One of the frequent requirement we receive is doing basic form customizations based on Form Type. E.g. Show few fields while creating record and hide those fields once record is saved.

First option which comes in developer’s mind

As we cannot check entity’s form type using Business Rules, we must use JavaScript to achieve this requirement. Hence, usually developer will write a JavaScript method and register an event on Form Load.

This will be time consuming task as compared to the solution which I have explained below. Also, it will need technical person to achieve this requirement, because it involves coding.

An option that usually skips from developer’s mind

As we know, Created On field is populated only when record is created. Also, CRM does not allow to set Created On field to the user, we can take help of this field to determine whether user is on Create form or has opened existing record.

Below screenshot shows how Business Rule can be configured to unlock the Account on Create form and lock it for existing record.

business rule

Simple solution, but we usually miss it.

Advertisements

Business Rules in CRM

What are Business Rules?

–          In CRM 2013, the Business Rules are introduced to allow end users to apply basic form logic without writing JavaScript in CRM

–          It provides simple declarative interface to implement


What things are possible using Business Rules?

–          Hide/Show fields on the form

–          Set field values

–          Enable/Disable fields

–          Set field requirements

–          Validate data and show error messages


4 ways to configure Business Rules

–          Solution -> Entity -> Business Rules

4.1

–          Solution -> Entity -> Field

4.2

–          Form Editor

4.3

–          Form Editor -> Field (Field Properties) -> Business Rules

4.4


How Business Rules work?

–          Business rules are applied based on the scope of the business rules selected during creation. Only activated business rules will be applied on the respective entity forms.

–          By default the scope is set to All Forms. To make the business rule available to specific form, you need to change the scope of the business rule.

5

–          In below order the logic is applied to the form field:

1. System Scripts

2. Custom JavaScript

3. Business Rules

Note: If there are multiple Business Rules applied on single field, then the rule in the order in which rules are activated. Hence if you want any business rule to be applied first, then you need to activate that rule at the last.


How to edit existing Business Rule?

To edit existing Business Rule, first deactivate the rule and then edit. To apply the updated logic, you will need to activate the rule again.


Advantages

–          Allows end user/ CRM customizer to apply logic quickly without writing any code

–          Enabling and Disabling of logic is simple

–          Easy to test the written logic by Preview option on the Form


Limitations

–          No complex logic is supported.

–          Business Rules does not run on Form Save event. They only run on Form Load and Field Changes.

–          Custom JavaScript event on the ‘OnChange’ event of field will not work if you set field value using Business Rules.

–          The rule will not run if the field mentioned in the rule is not available on the form. It will not throw an error. Hence user will not come to know about the error.

–          Business Rules does not work on Tabs/Sections/Sub Grids etc.

–          Does not allow else condition. Hence to apply If Else logic, you need to create separate business rules for each condition.

–          Only AND logic can be applied in the Business Rule. No OR logic.


How to setup logic in Business Rules?

Business Rules are divided into 3 sections:

–          Conditions

a. Conditions can be checked on fields only

b. Below operators are available

6

–          Actions

Actions are the tasks that can be performed by the Business Rules. Actions will be performed only if all conditions are true.

o   Show error message: Used to show any custom error message on any field based on the condition. This does not allow the form to save.

o   Set field value: Allows to set value to any field.

o   Set Business Required: Allows to set any field either required or not required.

o   Set visibility: Allows to show/hide the field.

o   Lock or Unlock field: Allows to make field read-only or editable.

–          Description (Optional)

Gives description of Business Rule. This is optional and does not appear anywhere else.


Examples

–          Assumptions:

I have custom entity named “Business Rules Demo” with below fields:

– Field 1 – Two Options

– Field 2 – Option Set { A, B, C, D }

– Field 3 – Single Line of Text

– Field 4 – Single Line of Text

– Field 5 – Single Line of Text

  1. If Field 1 = True

Then

Hide Field 3

Make Field 4 Required

Set Field 5 to ‘Hello’

Disable Field 2

Show error message on Field 1 as ‘Field 3 is hidden’

Else

Show Field 3

Make Field 4 not required

Set Field 5 to ‘Bye’

Enable Field 2

For this, we need to create 2 business rules; 1 for If part and other for Else part as below:

7

8

Note: To copy the business rule, open business rule and use ‘Save As’ option. This will create copy of the business rule.

Custom error message will be shown as below:

9

  1. To check if any field is blank or not, use Contains Data and Does Not Contain Data operator in the condition.

References

http://technet.microsoft.com/en-us/library/dn531086(v=crm.6).aspx