Sunday, January 21, 2024

Enabling/Disabling Button in Sub-grid based on field Value

Enabling/Disabling Button in Sub-grid based on field Value in Dynamics 365

Introduction

In Dynamics 365, there are often scenarios where you need to control the visibility of a button based on certain conditions. In this blog post, we will explore a common requirement: showing or hiding a button dynamically based on the value of a dropdown field. We will cover two approaches to achieve this, one using the Ribbon Workbench and the other utilizing the form scripting directly.


Refer to this video for a detailed guide on the previous setup steps for the form and Ribbon Workbench. Learn how to display buttons based on record selection in Ribbon Workbench and explore the process of unlinking entities (Account and Contact) on a button click within a sub-grid in Dynamics 365.
  1. Show Button based on records selection in Ribbon Work Bench 
  2. Unlink Entities -Account and contact on Button Click in Sub-grid

Ribbon Workbench Approach

The Challenge

Let's say we have a button that we want to show or hide based on the value of a dropdown field named "Account Status."

We can create a JavaScripot Webresource with a function, enableUnlinkButton, that takes executionContext as a parameter. This function will retrieve the form context using executionContext.getFormContext() and check the value of the "Account Status" field. (Not the right way for Ribbon Workbench Approach function ) Click here to download the code file

enableUnlinkButton(executionContext) { //Remove Debugger, this is only for testing purpose debugger; console.log("functioninvoked"); try { // Get the form context var formContext = executionContext.getFormContext(); var accountStatus = formContext.getAttribute("cr9f0_accountstatus1").getValue(); // Log the values to the console console.log("accountStatus value:", accountStatus); if (accountStatus != 459430002) { return true; } else{ return false; } } catch (error) { console.error("Error checking fields:", error); } }
Note -when this above function enableUnlinkButton(executionContext) is used within a Ribbon Workbench enable rule, it encounters an error: "TypeError: Cannot read properties of undefined (reading 'getFormContext')". This is because executionContext isn't available in the same way within the Ribbon Workbench context.) Scroll down to see the solution for this.

Configuration in Ribbon Workbench

  1. Select your custom button using the Ribbon Workbench.
  2. Associate the enableUnlinkButton function with the button's command.
  3. Use the CustomRule property of the button to specify the function.

Configuration in Ribbon Workbench involves the following steps:
1. Open Ribbon Workbench and access the Enable Rule section.


2. Create a new Enable Rule and click "Add Step."

3. Choose "Custom Rule."


4. Add the function name and corresponding JavaScript in the Custom rule.
Set Default Result to true and Invert Result to false.
This configuration ensures that the button will be enabled (true) when the conditions specified in the JavaScript function (i.e., accountStatus!== 459430002) are met. If the conditions are not met, the button will be disabled (false). The Invert Result is set to false to keep the logical flow consistent.


5. After setting up the rule, link it to your button command. Make sure your button always has a command attached, and you can manage all your enable/display rules inside this command.

5.Publish the solution

After completing these steps, your button should be visible in the account form under the contact subgrid. However, the logic is not yet added to the form. Follow the steps below:

7. Go to your form customization and add the previously created JavaScript to form properties.
8. Click "Add," search for your web resource, and add it.


9. Verify that the web resource is visible under form libraries. 


10. Publish the form
Now, the button is properly attached to the form and web resource.

Let's check its functionality to ensure it is working as intended.

Debugging:
Open the account form and activate developer mode by pressing the function + F12 key. In the developer mode sources, click on the next step in the debugger. You may encounter an error stating that the execution context is undefined, and none of the values are being fetched, it's likely due to the fact that executionContext isn't readily available in the Ribbon Workbench context.

To address this, utilize the "crm parameter primary control" in Ribbon Workbench and pass it directly into the form to retrieve the required field values. This adjustment ensures the seamless interaction of the Ribbon Workbench with the form, resolving the issue with the execution context being unavailable.

Understanding the executionContext Nuance

The enableUnlinkButton(executionContext) function works effectively when called directly on form load. It utilizes executionContext.getFormContext() to access the form's context and field values.
However, when this same function is used within a Ribbon Workbench enable rule, it encounters an error: "TypeError: Cannot read properties of undefined (reading 'getFormContext')". This is because executionContext isn't available in the same way within the Ribbon Workbench context.


Introducing primaryControl to the Rescue We introduce the enableUnlinkButton(primaryControl) function. It accepts the primaryControl parameter, passed from the Ribbon Workbench using a crm parameter. This function directly uses primaryControl to access the form context and retrieve the Account Status field value, successfully enabling or disabling the button as intended. 1. In Ribbon Workbench, navigate to the Custom Rule, click on "Add parameter," and choose CRM parameter.


2. Select "Primary Control" from the available options.

3. Ensure that it is placed within the same enable rule beneath your library, as demonstrated below.
4. Publish the solution to apply the changes.

The Reason for Using enableUnlinkButton(primaryControl)

The code function enableUnlinkButton(executionContext) displays "accountStatus value = 459430002" on form load. However, it will throw an error when the same function is accessed through Ribbon Workbench enable rule. So, to address this issue, we remove executionContext and add the CRM parameter in Ribbon Workbench. We then pass primaryControl to the function.
Modify the Javascript and publish it Click here to download the code file
function enableUnlinkButton(primaryControl) { console.log("functioninvoked"); debugger; try { var formContext = primaryControl; // Get the form context var accountStatus = primaryControl.getAttribute("cr9f0_accountstatus1").getValue(); // Log the values to the console console.log("accountStatus value:", accountStatus); if (accountStatus !== 459430002) { return true; } else{ return false; } } catch (error) { console.error("Error checking fields:", error); } } Debugging
Open the account form and activate developer mode by pressing the function + F12 key. In the developer mode sources, click on the next step in the debugger. You may esee that now the formContect and field value is fetched correctly

Call this function within appropriate events to dynamically enable or disable the button:
  • On deposit status change: accountStatusaddOnChange(enableUnlinkButton);

Conclusion

In this blog post, we explored two approaches to dynamically enabling or disabling a button based on the value of a dropdown field in Dynamics 365. The Ribbon Workbench approach is suitable for scenarios where customization is done through the Ribbon Workbench, while the Form Context approach is more direct and can be used when scripting directly in the form designer.

Remember to choose the approach that best fits your specific scenario and development workflow. Feel free to experiment and adapt these solutions to meet your unique requirements. Happy coding!

Related blog-
  1. Show Button based on records selection in Ribbon Work Bench 
  2. Unlink Entities -Account and contact on Button Click in Sub-grid

No comments:

Post a Comment

Followers

Power Dynamix YouTube Videos