Compartilhar via


Create and edit business rules

 

Applies To: Dynamics CRM 2013

With Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online, you can apply form logic without writing JavaScript code. Business rules provide a simple declarative interface to implement and maintain fast changing, commonly used business rules that will be applied to Main and Quick Create forms for both the web application and Microsoft Dynamics CRM for tablets.

In This Topic

Why business rules?

What can business rules do?

How do I configure business rules?

Limitations for business rules

Localize error messages used in business rules

Why business rules?

Not every organization has a developer readily available to apply changes when the rules in an organization change. Workflows allow a non-developer to apply logic, but this logic can be applied only on the server, after a record has been created or updated. Client-side logic is more immediate because it can be performed as someone creates or edits a record.

What can business rules do?

Business rules allow for a subset of the capabilities provided by form scripts. With Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online, you can define conditions and apply the following actions:

  • Set field values

  • Set field requirement levels

  • Show or hide fields

  • Enable or disable fields

  • Validate data and show error messages

Business rules can be set to apply to all Main or Quick Create entity forms or specific Main forms that you choose.

You can transport business rules from one organization to another by including them in a solution and you can install solutions that contain business rules.

How do I configure business rules?

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.

    Business Rules on the account entity

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

    Business Rules on a field

  • 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

    Business Rules for the Form

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

    Business Rules on Form Field

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 to All Forms or choose any one of the Main forms. 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.

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 chose 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, click or tap 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 click the Delete icon.

Configure actions

To add an action, click the + icon 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.

    Error message on a field in the form

    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.

Limitations for business rules

Business rules in this release 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.

The primary limitation you may find compared to form scripts is that all conditions in the business rules are evaluated using AND. All the conditions must be true before the actions will be applied. There is no support for OR or Else operators to provide for more complex logic. To apply OR in your conditions, you need to create separate rules for each condition you want to test. This can be done efficiently by using the Save As option and creating separate rules for each condition you want to test.

Here are some other 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.

  • 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 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 CRM for tablets, the definition of the business rules are downloaded and cached when CRM for tablets opens. Changes made to business rules are not applied until CRM 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 CRM, so that people using languages other than your base language can view the translated messages.

See Also

Video: Microsoft Dynamics CRM 2013 Customization New Features - Business Rules
Create and edit processes
Create and design forms
Create and edit views
SDK Topic:Create or edit how business rules are initiated