Firstly, let’s delve into Business rules and when they’re applicable. Business rules in the Power platform are primarily used to provide declarative logic for model-driven Power Apps. They are great for:

  • Setting field values: Populate data in certain fields based on what a user inputs in other fields.
  • Clearing field values: Delete the value of a field based on what a user inputs in another field.
  • Validating data and showing error messages: For instance, you may want to verify whether data entered in a specific field adheres to certain criteria, and if it doesn’t, post an error message.
  • Setting field requirement levels: Change the requirement level of specific fields based on what a user inputs in other fields.
  • Showing or hiding fields: Different fields can pop up depending on the data input in other fields.
  • Enabling or disabling fields: Users will only be able to interact with specific fields depending on their input in other fields.

Here is an example of using business rules:

Let’s say we have a form where a user can select a product. If the chosen product has an expiry date, it should be mandatory to fill the ‘Expiry Date’ field. This can be achieved with a business rule. Additionally, if a product is marked as ‘discounted’, the ‘Discount Rate’ field should appear. Again, a business rule can help make this happen.

Table of Contents

Client Script: When To Use

Meanwhile, JavaScript or Client Script is used on the platform to manipulate the form or interact with the server. It provides more control over the form behavior but demands a higher skill level to implement. Client scripting strikes when:

  • You want to add custom behavior based on form events.
  • You need to interact with server-based data without a page refresh.
  • Richer validation is required, based on business logic that is far too complex for business rules.
  • You need to show or hide sections or tabs.
  • You need to change the label of a field.
  • You have to manipulate the lookup field’s view.

Let’s take an example where there is a need to change a label or hide a section based upon the logged-in user’s role. This cannot be achieved by business rules.

Here’s a simple code example to change field labels:

Xrm.Page.getControl("fieldName").setLabel("New Label");

And to hide a section based on role, this would work:

var section = Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname");

if (currentUserHasRole) {
section.setVisible(true);
} else {
section.setVisible(false);
}

Business Rules vs Client Scripting: A Comparison

Feature Business Rules Client Scripting
Set field values Yes Yes
Clear field values Yes Yes
Validate data and show error messages Yes Yes, more powerful
Set field requirement levels Yes Yes
Show/Hide fields Yes Yes
Enable/Disable fields Yes Yes
Complex Business Logic No Yes
Change field Label No Yes
Show/Hide Sections or Tabs No Yes
Server Interaction No Yes

Knowing when to use business rules or client scripting can significantly impact how well you design and develop your Power Platform applications. While business rules tend to simplify form adjustments with distinct triggers, client scripting provides more control and complexity, capable of achieving much more advanced functions. It comes down to the specific requirements of your project on whether to use business rules or client scripting. By understanding these tools, you’ll be well-prepared for the PL-400 Microsoft Power Platform Developer exam.

Practice Test

True or False: Business rules always run on the client-side, not the server-side.

  • True
  • False

Answer: False

Explanation: Business rules can run both client-side and server-side. They are designed to provide a simplified interface for defining business logic, that can then be executed from the client or the server.

True or False: Client scripting is typically used for form customization and validation.

  • True
  • False

Answer: True

Explanation: Client scripting is often used for tasks such as form customization, data manipulation, and field validation. It is run directly in the user’s browser.

Multiple Select: Which of the following are benefits of using business rules?

  • a) Easy to implement
  • b) Do not require knowledge of JavaScript
  • c) Can be run on both client-side and server-side
  • d) Business rules are useful for complex business logic

Answer: a, b, and c

Explanation: Business rules are easy to implement, do not require knowledge of JavaScript, and can be run on both client-side and server-side. While they are useful for basic business logic, for complex logic, other methods such as plugins or workflows are recommended.

Single Select: When you need to create a complex business process, which is typically the best to use?

  • a) Client scripting
  • b) Business rules
  • c) Plugins
  • d) Workflows

Answer: c) Plugins

Explanation: For complex business processes, plugins are typically the best choice because they offer more flexibility and can handle more complex operations.

True or False: Client scripting can also be used for server-side operations.

  • True
  • False

Answer: False

Explanation: Client scripting specifically refers to scripts that are run on the client-side, or in the user’s browser.

Single Select: Which is more suitable for basic form validation?

  • a) Business rules
  • b) Client scripting
  • c) Server-side scripting
  • d) Workflows

Answer: b) Client scripting

Explanation: Client scripting is well-suited to tasks like data manipulation and form validation.

True or False: Business rules continue to function even if the user has disabled JavaScript in their browser.

  • True
  • False

Answer: True

Explanation: Business rules, being server-side, do not depend on the user’s browser settings and will function regardless of whether JavaScript is enabled or not.

Multiple Select: Which of the following scenarios are suitable for using Client scripting?

  • a) Data manipulation
  • b) Form customization
  • c) Complex business logic
  • d) Simple data validation

Answer: a, b, and d

Explanation: Client scripting is well suited to tasks such as form customization, data manipulation and simple data validation. It is not typically used for complex business logic.

Single Select: Which is better to use when quick performance is required?

  • a) Client scripting
  • b) Business rules
  • c) Server-side scripting
  • d) Workflows

Answer: a) Client scripting

Explanation: Since client scripting runs directly on the user’s browser, it generally provides quicker performance as compared to server-side operations.

True or False: Business rules do not allow for any sort of scripting.

  • True
  • False

Answer: True

Explanation: Business rules are a no-code feature of the Power Platform. This means they provide a graphical interface to define business logic, rather than requiring a script to be written.

Interview Questions

What is a Business Rule in Microsoft Power Platform?

In Microsoft Power Platform, a business rule is a declarative rule defined within a Common Data Service entity to provide server-side logic for data manipulation.

What is Client Scripting in Microsoft Power Platform?

Client scripting in Microsoft Power Platform involves writing JavaScript code that executes in response to form events, such as a field changing value or the form loading.

When might it be better to use a business rule rather than client scripting in Microsoft Power Platform?

Business rules could be better when the logic is simple and needs to be enforced consistently across all platforms. As business rules are server-side, they are executed regardless of the client.

When would client scripting be preferable over business rules in Microsoft Power Platform?

Client scripting may be a better choice if your logic is complex, requires interaction with users, or needs to be executed promptly in response to form events.

Do business rules or client scripting execute first in form events in Microsoft Power Platform?

Business rules execute before client scripting when a form loads.

Can business rules be used with an offline client in Microsoft Power Platform?

Yes, business rules also apply in an offline client as long as the client has synchronized with the server.

Can client scripting be used with an offline client in Microsoft Power Platform?

Yes, however, not all client-side code will function as expected offline. It requires careful design and testing to implement offline-compatible client scripting.

Can business rules work with other entities in Microsoft Power Platform?

No, business rules can only operate within the scope of a single entity.

How many actions or conditions can a business rule have in Microsoft Power Platform?

There is no defined limit to the number of conditions or actions that a business rule can have. However, complex business rules might affect system performance.

Can a business rule set field values in Microsoft Power Platform?

Yes, business rules can set field values, show or hide fields, enable or disable fields, and validate data and show error messages.

Can client scripting get or set lookup value in Microsoft Power Platform?

Yes, client scripting can get or set lookup value using Xrm.Page.data.entity.attributes.get and Xrm.Page.data.entity.attributes.set methods.

What happens when a business rule and client script conflict in Microsoft Power Platform?

The result is unpredictable and depends on the sequence of execution. It is recommended to avoid such conflicts.

Can client scripting interact with web resources on a form in Microsoft Power Platform?

Yes, client scripting can interact with web resources on a form. For example, the script can get a reference to an iframe or a web resource control on the form.

Can a business rule interact with web resources on a form in Microsoft Power Platform?

No, business rules cannot interact with web resources on a form.

Can business rules and client scripting coexist on the same form in Microsoft Power Platform?

Yes, business rules and client scripting can coexist on the same form, but care must be taken to avoid conflicts or unexpected behavior.

Leave a Reply

Your email address will not be published. Required fields are marked *