Enabling/Disabling Button in Sub-grid based on field Value in Dynamics 365
Introduction
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
- Select your custom button using the Ribbon Workbench.
- Associate the
enableUnlinkButton
function with the button's command. - 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."
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:
Now, the button is properly attached to the form and web resource.
Let's check its functionality to ensure it is working as intended.
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.
The Reason for Using enableUnlinkButton(primaryControl)
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.
- 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-