Wednesday, April 17, 2024

Business Rules to show Error Messages in Dynamics 365 / Power Platform

Business Rules to show Error Messages in Dynamics 365 / Power Platform

Introduction:

This blog will show you how to use Business Rules in Dynamics 365 to stop users from saving records and display error messages. While you could use JavaScript or Plugins for this, Business Rules win out because they're easier to use, don't require coding, and are quicker to set up.

Scenario: We will create a business rule in Dynamics 365 to show an error message if a from date field is greater than To date.

Refer to this video for a detailed implementation guide for creating business rule-








What are Business Rules?

In Dynamics 365, a business rule is a feature that allows you to implement logic on forms without needing to write code. It provides a user-friendly drag-and-drop interface for creating rules that automate specific actions based on certain conditions.

Here are some key points about business rules in Dynamics 365:

Functionality: They can perform various actions like setting field values, validating data, showing or hiding fields, and making fields required or optional based on conditions.

Benefits: Business rules are beneficial because they are:

  •     Easy to use with a drag-and-drop interface
  •     Quick to implement compared to code
  •     Require no coding knowledge for creation or maintenance
  •     Run in real-time, providing immediate feedback on form changes

Scope: You can define the scope of a business rule to apply to specific forms, all forms for an entity, or even run server-side without requiring a user to be on a form.

Here's how they work:

Conditions: Business rules are triggered based on conditions defined by the user. These conditions typically involve comparing field values or checking the state of certain fields.

Actions: When the conditions are met, business rules execute specific actions. These actions can include setting field values, making fields required or read-only, showing or hiding fields, and even displaying error messages.

Scope: Business rules can be applied at various levels, including entity level, form level, or field level, allowing for flexibility in implementation.

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.

Advantages of Business Rule:

No Coding Required: Business rules allow you to implement this logic without writing any code. This is beneficial for users who may not have coding skills but still need to customize Dynamics 365.

Ease of Maintenance: Business rules are easily manageable through the Dynamics 365 interface. You can modify them without the need for deploying code changes, making maintenance simpler and faster.

User-Friendly: Configuring a business rule through the Dynamics 365 interface is intuitive and user-friendly. Business analysts or power users can quickly create and modify rules without involving developers.

Built-in Validation: Dynamics 365 provides built-in support for showing error messages using business rules. When the condition is met, the error message is displayed directly on the form, guiding users and ensuring data integrity.

Performance: Business rules execute client-side, meaning they run directly in the user's browser. This can result in faster response times compared to server-side logic like plugins, which need to communicate with the Dynamics 365 server.

Scalability: Business rules are scalable and can handle various conditions and actions without significant performance impact. They're suitable for a wide range of customization scenarios.

JavaScript/Plugin Disadvantages:

Coding Required: JavaScript and plugins require writing and deploying code, which can be time-consuming and introduce potential errors.

Slower Implementation: Development and deployment take longer compared to a simple business rule.

Maintenance Overhead: If the validation logic changes, you'll need to modify the code and redeploy, increasing maintenance overhead.

Overall, using a business rule for this scenario provides a simple, efficient, and user-friendly solution that meets your requirement without the need for custom code. It's a great way to leverage Dynamics 365's customization capabilities to enforce business logic effectively.

Here's how to create a business rule for this scenario

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

  1. Go to the entity where your date field resides.
  2. Open the solution and go to your entity where you want to create business rule and click on New Business Rule.


  3. Give your business rule a clear name something like "Validate Date Against Today".

  4. Choose the scope (form or entity level) and when the rule should run (on save).

  5. Click on conditions, give name, create rule and enter proper fields. Click on Apply every time you create an action or condition.


  6. Now, click on components and drag "Show Error Message" from the action tab to a plus sign in the designer.

  7. Under properties of "Show Error Message", lick the Condition component in the designer window, and then set the properties like name and select field on which you want to show error message if condition is not met on the right side of the screen. Click on Apply. 

  8. Go to components, select Condition and drag below the first condition. Here you can more condition like earlier.

  9. Now, click on components and drag "Show Error Message" right to condition.
    Under properties of "Show Error Message", give name and select field on which you want to show error message like above steps. Click on Apply

  10. Click on Save button and then click on Validate
  11. Finally activate the business rule.
Go to your for or records to test the business rule working.

Importance of Scope- 
Understanding the scope levels of Business Rules is essential for determining when they will be triggered. There are three levels of scope when setting up Business Rules in Microsoft Dynamics 365 -

Specific Form: At the lowest level of scope is the Specific Form setting. Each Main Form in Dynamics 365 can have its own set of Business Rules. These Business Rules will only run on the form they are configured for. This level of scope provides flexibility, allowing organizations to tailor user experiences based on different forms.

All Forms: The All Forms scope applies a Business Rule to every form within Dynamics 365. However, users must be on a form for the Business Rule to execute. This level of scope offers broader application across various forms while still requiring user interaction for activation.

Entity: The Entity scope represents the highest level of scope for Business Rules. Business Rules set at the Entity level run on the server side, independent of any specific form. Users do not need to be on a form for these Business Rules to execute. This level of scope is ideal for scenarios where consistent logic needs to be enforced across all interactions with the entity.

Reference - Microsoft learn

Tips-

  • If you want to modify an existing business rule, you must deactivate it before you can modify it.
  • 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.

No comments:

Post a Comment

Followers

Power Dynamix YouTube Videos