Since CRM 2013, we can apply our business logic in CRM without writing even a single line of JavaScript code or developing plugins. This feature called business rules provide a simple interface to implement and maintain fast changing, commonly used business rules.
These business rules can be either applied to a form (Main, Quick Create) or to an entity. This will be applicable for web application, tablets and even for outlook clients in both online and offline modes.
Business rules can run both on client and server (this is a feature from 2015, in 2013 it was only for client). If the rule’s scope is set at a form level (Can be set for a specific form or for all forms. Its one or all, if you want to select two out of three forms available, you have to copy one rule and set for another form specifically), the rules are executed when form is loaded and record is updated. If the scope of the record is set to entity, then rule will be applied on server and will be executed when record is created or saved.
Now we know when and where the business rules can be called. Lets look at what they are capable of. We can apply conditions and several actions as follows,
- Set field values
- Clear field values
- Set field requirement levels
- Show or hide fields
- Enable or disable fields
- Validate data and show error messages.
Once they are configured business rules can be moved from one organization to another by including them in a CRM solution and importing to the other organization.
Once we know what these business rules can do, its better to know who can configure or create new business rules. Typically a system administrator or a system customizer can configure or create a new business rule. For activating the business rule, its necessary to have “Activate Business Rule” privilege. Ideally above mentioned roles should have that privilege.
Microsoft itself prevents us from having a circular reference to a field, if the scope of the rule is set as entity. This is because the rule will be checked twice once at client and other at server leading to do the business logic check twice. So if this is related to some calculations the value may go wrong.
We can create complex logic in business rules by using the AND operator. All conditions had to be true for an action to happen in this case. This is a new feature from CRM 2015 and for 2013 we dont had this luxury. Even with this luxury there are some limitations for the time being,
- Nested If – else statements are not supported.
- Grouping of expressions in a condition is not supported.
- Expressions can be combined either using AND, or using OR, but not both.
Reading all this might have made you think that this is really cool and can replace the JavaScript in this case. Yes this feature is really cool but this feature cannot replace JavaScript all together. Business rules have some limitations which makes sure that the JavaScript cannot be completely ignored.
- Business rules run only when the form loads and when field value change. They do not run when a record is saved. An exception to this would be when the scope of the rule is set to entity level.
- Business rules only work with fields. If we need to interact with other visible elements, such as tabs, sections, we need to use scripts.
- If we set a value using business rule, any onchange event handlers for that field will not run. This is just to reduce the potential for a circular reference, which could lead to an infinite loop.
- For a business rule to run, the field should be available in the form. Else the rule will not run. There will be no error message as well.
- Whole number fields that use the formats for TimeZone, Duration or Language will not appear in the rule editor for conditions or actions. This prevents them from being used in business rules.
- For tablets these rules are downloaded and cached, so changes made to rules are not applied until the CRM for tablet is closed and reopened.
- When we set a lookup field value using business rule, make sure to update the rule if the text value of the lookup changes. If we didnt do this then the value set by the rule will be correct except the text portion of lookup.
Except for showing error messages, we must be sure to include a separate business rule to clear any actions we may have applied. Show error messages is an exception in this case as it automatically evaluates any conditions used to set the error message and will remove if the conditions are no longer valid.
Hope you got a grip on business rules. One more thing to keep in mind is the order in which these business rules will be executed.
- Any system scripts are applied first.
- Any logic in custom form scripts is applied.
- Logic in business rule is applied. If multiple business rules are available, they are applied in the order they we activated, from oldest to newest.
- The entity scoped business rules are executed in the server after asynchronous plugins.
Business rules provides options to localize the messages for the users in a multi lingual set up to use the local language for the user. For this the user has to add localized versions of the messages and then import the solution back to CRM.