Share via


Create business rules and recommendations to apply logic in a form

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2013, Dynamics CRM 2015, Dynamics CRM Online, Dynamics CRM 2016

You can create business rules and recommendations to apply form logic without writing JavaScript code or creating plug-ins. Business rules provide a simple interface to implement and maintain fast-changing and commonly used rules. They can be applied to Main and Quick Create forms, and they work in Microsoft Dynamics 365 for tablets and Dynamics 365 for Outlook (online or offline mode).

By combining conditions and actions, you can do any of the following with business rules:

  • Set field values

  • Clear field values

  • Set field requirement levels

  • Show or hide fields

  • Enable or disable fields

  • Validate data and show error messages

  • Create business recommendations based on business intelligence.

    Note

    To create business recommendations, you must be using the December 2016 update for Dynamics 365 (online and on-premises) or later.

How you create a business rule depends on whether you're using Dynamics 365 or Dynamics CRM. If you're using Dynamics 365, you can use a new drag-and-drop designer to create business rules. You must have the December 2016 update for Dynamics 365 (online and on-premises) to use Dynamics 365.

Create a business rule or business recommendation in Dynamics 365

  1. Make sure that you have the System Administrator or System Customizer security role or equivalent permissions.

    Check your security role

    1. Follow the steps in View your user profile.

    2. Don’t have the correct permissions? Contact your system administrator.

  2. Go to Settings>Customizations.

  3. Click Customize the System.

  4. Open the entity you want to create the business rule for (for example, open the Account entity), and then double-click Business Rules.

    Create a business rule in the default solution

  5. Click New.

    The Business Rule designer window opens with a single condition already created for you. Every rule starts with a condition. The business rule takes one or more actions based on that condition.

    Business Rules design window

    Tip

    If you want to modify an existing business rule, you must deactivate it before you can modify it.

  6. Add a description, if you want, in the description box in the upper-left corner of the window.

  7. Set the scope, according to the following:

    If you select this item...

    The scope is set to...

    Entity

    All forms and server

    All Forms

    All forms

    Specific form (Account form, for example)

    Just that form

  8. Add conditions. To add more conditions to your business rule:

    1. Drag the Condition component from the Components tab to a plus sign in the designer.

      Add a condition in a business rule

    2. To set properties for the condition, click the Condition component in the designer window, and then set the properties in the Properties tab on the right side of the screen. As you set properties, Dynamics 365 creates an expression at the bottom of the Properties tab.

    3. To add an additional clause (an AND or OR) to the condition, click New in the Properties tab to create a new rule, and then set the properties for that rule. In the Rule Logic field, you can specify whether to add the new rule as an AND or an OR.

      Add a new rule to a condition

    4. When you're done setting properties for the condition, click Apply.

  9. Add actions. To add an action:

    1. Drag one of the action components from the Components tab to a plus sign next to Condition component. Drag the action to a plus sign next to a check mark if you want the business rule to take that action when the condition is met, or to a plus sign next to an x if you want the business rule to take that action if the condition is not met.

      Drag an action to a business rule

    2. To set properties for the action, click the Action component in the designer window, and then set the properties in the Properties tab.

    3. When you're done setting properties, click Apply.

  10. Add a business recommendation. To add a business recommendation:

    1. Drag the Recommendation component from the Components tab to a plus sign next to a Condition component. Drag the Recommendation component to a plus sign next to a check mark if you want the business rule to take that action when the condition is met, or to a plus sign next to an x if you want the business rule to take that action if the condition is not met.

    2. To set properties for the recommendation, click the Recommendation component in the designer window, and then set the properties in the Properties tab.

    3. To add more actions to the recommendation, drag them from the Components tab, and then set properties for each action in the Properties tab.

      Note

      When you create a recommendation, Dynamics 365 adds a single action by default. To see all the actions in a recommendation, click Details on the Recommendation component.

    4. When you're done setting properties, click Apply.

  11. To validate the business rule, click Validate on the action bar.

  12. To save the business rule, click Save on the action bar.

  13. To activate the business rule, select it in the Solution Explorer window, and then click Activate. You can't activate the business rule from the designer window.

Tip

Here are a few tips to keep in mind as you work on business rules in the designer window:

  • To take a snapshot of everything in the Business Rule window, click Snapshot on the action bar. This is useful, for example, if you want to share and get comments on the business rule from a team member.

  • Use the mini-map to navigate quickly to different parts of the process. This is useful when you have a complicated process that scrolls off the screen.

  • As you add conditions, Actions, and business recommendations to your business rule, Dynamics 365 builds the code for the business rule at the bottom of the designer window. This code is ready only.

Create a business rule in Dynamics CRM

First, you need to have the privileges necessary to navigate to Settings > Customization. This typically requires the System Administrator or System Customizer security role. To activate a business rule, you must have the Activate Business Rules privilege.

Note

Business rules will only work for updated entities or custom entities.

There are four ways you can view, create, or edit business rules:

  • Solution > Entity
    From a solution, such as the default solution, you will find a Business Rules node for all entities.

    Create a business rule, method 1

  • Solution > Entity > Field
    When you view an entity field, you will find a Business Rules node that will show you only the business rules that include this attribute.

    Create a business rule, method 2

  • Form Editor
    From the form editor, you can use the Business Rules button in the ribbon to show the Business Rules Explorer on the right side. This will show you all business rules that will be applied just for this form.

    If you create a rule from the form editor, the default scope is for that form. More information:  Set the scope

    Create a business rule, method 3

  • Form Editor > Field
    When you view the properties for a field that is used in a form, you will see a Business Rules tab that shows you the business rules that include this attribute.

    Create a business rule, method 4

If an existing rule is similar to a rule you want to make, you can open that rule and use the Save As button to copy an existing rule as a starting point for a new rule.

Set the scope

In the top right of the form, use the Scope field to set the scope for the rule.

Scope

Where it runs run

Entity

All forms and server

All forms

All forms

Specific form

Just that form

You cannot select multiple specific forms. If you choose All Forms, the rule will be applied to all the Main forms and the Quick Create form, as long as the form includes all the fields referenced by the rule. If you create a new business rule by using the form editor, the default scope is just that form.

Server side business rules and support for IF-Else and AND/OR logic

Previously, you had an ability to evaluate the business rules on an individual client. To evaluate the business rule logic on the server and apply it to all clients, you had to provide the plug-ins, which are expensive to develop and maintain. Setting the scope of the business rule at an entity level, gives you an ability to evaluate the business rule once on the server and apply it to all clients without writing code. You can move the logic for commonly used scenarios out of plug-ins into the entity-level business rules. In addition, we provided the support for default values in a business rule. For instance, if Contoso only does business in the United States, a simple business rule can be implemented that on creation of an incoming lead, the country/region is automatically set to U.S.A.

Note

When the scope is set to an Entity and you create or edit a record using the forms, the rule runs on the client side, but later, it runs again on the server. Because of this, we prevent you from creating a circular reference to a field, if you set the scope to an Entity. For example, you can't set Credit Limit = Credit Limit + 1000, because it would increase the value once on the client side and then would try to run again on the server side.

For more complex business scenarios, you needed to use multiple business rules that were evaluated using the AND logic operator. All conditions had to be true before the actions were applied. There was no If-Else or AND/OR support. This functionality is now added and will let you create more elaborate business rules with less effort. The following example of the business rule uses the If-Else logic:

Business rule with If Else logic

There are a few limitations that you should be aware of:

  • 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.

Configure conditions

If you want to change an activated business rule, you must deactivate it before you can edit it.

To add a condition, click the + icon and a new condition row will appear with default values set. Enter the field name to set the Field, and then choose the appropriate Operator. Operator options will change depending on the data type of the field.

Conditions are checked whenever any field referenced within the condition changes.

You can choose three different types of conditions:

  • Field
    Use this type to compare the value of one form field with another.

  • Value
    Use this type to compare the value of one form field with a value you enter.

  • Formula
    This option appears only for numerical or date data types. It does not appear for fields that contain text. Use this type to compare the result of a simple calculation that may use either a value in another form field or a value you enter.

When you are finished entering or editing the rule, choose the check mark icon to save it or the (X) icon to discard changes. To remove a previously saved condition, place your cursor over the condition and choose the Delete button .

Configure actions

To add an action, choose the Add button + and you will have the following options:

  • Show error message
    Use this action to set an error message on a field if the data within it is not valid. The text you specify for the message will be displayed with an error icon near the field.

    Business rule, show error message

    The record cannot be saved as long as this message is displayed. After the data in the field has been corrected according to the conditions set in your rule, the message will disappear and the record can be saved.

  • Set field value
    Choose the Field and then the Type. There are three types:

    • Field
      Use this type to set the value of one form field with the value of another field.

    • Value
      Use this type to set the value of a form field with a value you enter.

    • Formula
      This option appears only for numerical or date data types. It does not appear for fields that contain text. Use this type to set the value to the result of a simple calculation that may use either a value in another form field or a value you enter.

  • Set business required
    Use this type to change the requirement level for the field. The options are Not Business Required and Business Required. There is no option to set this to business recommended.

  • Set visibility
    Use this type to change whether the field is displayed in the form. The options are Show Field and Hide Field.

  • Lock or unlock field
    Use this type to change whether the field is enabled in the form. The options are Lock and Unlock. When the field is locked, people will not be able to edit the value in the field.

After you have defined an action, you can change the order or delete it by using the options available when you place your cursor over the action.

Set the description

Setting a description is optional. It isn’t displayed anywhere else except in the business rule editor. But it is a good idea to include a description of what the rule is supposed to do and why it has been added.

Test and activate your business rules

Before anyone can use the business rules you have created, you must activate them. Before you activate them, you should test them. You can test business rules by using the Preview button in the form editor.

Examples of setting or clearing field values

Let’s consider a couple of examples. With the first rule, for any immediate purchase, you apply a 5% discount. With the second rule, you clear all discounts if the purchase timeframe is unknown,

The following rule definition shows how to set a field value to a 5% discount for immediate purchases.

Business rule that sets a value to a 5% discount

The following rule definition shows how to clear a field value – clear all discounts, for purchases with an unknown purchase timeframe.

Business rule that clears a field value

Limitations

Business rules are intended to address common actions. Compared to what a developer can do by using form scripts, business rules have limitations. However, business rules are not intended to replace form scripts.

Here are a few limitations to using business rules:

  • Business rules run only when the form loads and when field values change. They do not run when a record is saved, unless the scope for the rule is set at an entity level.

  • Business rules work only with fields. If you need to interact with other visible elements, such as tabs and sections, within the form you need use form scripts.

  • When you set a field value by using a business rule, any OnChange event handlers for that field will not run. This is to reduce the potential for a circular reference, which could lead to an infinite loop.

  • If a business rule references a field that is not present on a form, the rule will simply not run. There will be no error message.

  • Whole Number fields that use the formats for TimeZone, Duration, or Language will not appear in the rule editor for the conditions or actions, so they cannot be used with business rules.

  • You can’t add more than ten if-else conditions in a business rule.

  • For Microsoft Dynamics 365 for tablets, the definition of the business rules are downloaded and cached when Dynamics 365 for tablets opens. Changes made to business rules are not applied until Dynamics 365 for tablets is closed and re-opened.

  • When you set the value of a lookup field, the text of the primary field value that is set in the form will always match the text that is visible in the rule definition. If the text representing the primary field value of the record you are setting in the lookup changes, the value set by your rule will continue to use the text portion of the primary field value defined by the rule. To fix this, update the rule definition to use the current primary name field value.

    It is useful to understand that the value set for a lookup has three parts:

    • Name: The text of the primary field value you see in the form.

    • Id: The unique identifier for the record. This is the data that is saved. This is not visible in the form.

    • LogicalName: The name of the entity, such as contact, account, or opportunity.

    The rule will set all three parts of this value. The Id value for a specific record never changes, but the Name value might change.

    For example, if you define a rule to set a lookup to a contact that has the Full Name of ‘Old Name’, this text is the Name you will see in the lookup when it is set by your business rule even if someone later changes the Full Name of the contact to ‘New Name’. The lookup Id value will be correctly set to the expected record, but the Name (which is not saved) will reflect the rule definition value rather than the current Full Name value of the record it references.

Clear the actions from your rules

Except for showing error messages, you must be sure to include a separate business rule to clear any actions you may have applied. For example, let’s say you have a rule on an Account entity that says:

If Preferred Method of Contact equals “Phone
Set Business Phone as Business Required

This will work whenever the Preferred Method of Contact field is set to Phone. But if the Preferred Method of Contact field is later changed to something else, the Phone field will remain Business Required. In order to clear this action, you need to include a separate rule to apply the following logic:

If Preferred Method of Contact does not equal “Phone
Set Business Phone as Not Business Required

The Show error message action is different because it automatically evaluates any conditions used to set the error message and will remove it if the conditions are no longer true.

Localize error messages used in business rules

If you have more than one language provisioned for your organization, you will want to localize any error messages that you have set. Each time you set a message, a label is generated by the system. If you export the translations in your organization, you can add localized versions of your messages and then import those labels back into Microsoft Dynamics 365, so that people using languages other than your base language can view the translated messages.

See Also

Guide staff through common tasks with processes
Create a business process flow
Create a mobile task flow