Business rules help us to implement business logic without any coding requirement. System administrators or business users can use the out-of-the-box business rule designer to design business specific validations in a declarative manner.
Business rules were initially released in Microsoft Dynamics CRM 2013, but we got new enhancements in Microsoft Dynamics CRM 2015. Business rules can be used for simply setting default value or doing complex calculations using formulas. While creating a business rule, we can define its scope. We have the flexibility of applying business rules to specific entity forms or all entity forms (if we are using multiple entity forms).
Business rules are portable so we can take them from one environment to another. When we include our entity in the solution, business rules are also included in it. We can create business rules from different places such as from entity forms, field property forms, and entity trees.
While creating business rules we will be getting the business rule designer as follows, where we can define conditions and their corresponding actions. We can use the Scope drop-down to select options for our business rule scope.
Business rules can be only applied to entity fields, currently we can't use business rules for other controls. Microsoft Dynamics CRM 2015 allow us to write both server-side and client-side business rules. We will be discussing more on this in later topic. Client-side business rules are executed on OnChange
of the field and OnLoad
of entity form.
We can use business rules for different requirements. Following are the six business rule actions that we can use for applying business validations.
We can show an error message using a business rule when any business-specific validations fails. Let's take an example in our training solution: the Job Title field should be completed by the user if Contact Type is employee
. We can design our business rule like following to display an error message on the Job Title field:
Once activated, it will show an error message as follows if the Job Title is blank for an employee.
We can also send business requirements using business rules. It may be that, if a specific field is selected, we dynamically want to make some another field required. For instance, in the previous example we can add another action to make the Job Title field required. But we also need to remove the requirement level if Contact Type is not Employee as follows:
Now it will also show an error message and will remove the requirement level if Contact Type is not Employee or Job Title is completed by the user.
Business rules also contain actions for setting field values. To set a field value we can utilize three options—value, field, or formula—based on the field datatype. We can assign specific values to the target field or we can select a field option to copy another similar datatype field value to the target field. We can set values using the following options:
Now we don't need to write JavaScript to hide the field; we can use business rules to set the visibility of the field based on a condition.
Another new enhancement was added in Microsoft Dynamics CRM 2015 to business rules. We can use business rules to set up default values when a record is created. For example you may want to set a pricelist as the default. Keep in mind, while you are deploying business rules from one environment to another, that the default lookup will only work if a source record exists in the target system with the same GUID.
Initially, business rules were only available to run on the client-side, but Microsoft Dynamics CRM 2015 added the ability to run business rules on the server-side as well. If business rules are activated using the Entity scope they are executed on both the server-side and client-side.
For example if we have created a business rule to set up some fields and activated our business rule using the Entity option, it will be executed when we try to create a record using the CRM SDK or through any other server-side process.
When any business rule scope is set to Entity and an entity record is created or edited using entity forms, the business rule will be executed twice, the first time on the client and the second time on the server. Because of this, CRM won't allow us to create a circular reference to the field if the business scope is Entity.
For example, in the preceding example we are using the Annual Revenue field on both sides and the scope of the business rule is Entity. This will double the value of the Annual Revenue field, so CRM won't allow us to activate this business rule. When we try to activate this rule, it will show an error as follows:
3.15.137.75