Skip to main content
Dynamics 365
·
2 min read

Disable All Fields in a Section Based on the Value of Another Field (CRM 2011)

imageToday’s guest blogger is Matt Wittemann, author of the Microsoft Dynamics CRM 2011 Administration Bible (Wiley, 2011), a six-time recipient of the Microsoft Most Valuable Professional (MVP) award for Dynamics CRM, and is the Director of Solution Architecture for US-based C5 Insight.

There are SDK and web examples of how to disable or hide an entire tab on a form in Microsoft Dynamics CRM 2011, but I was unable to find an example of how to just disable all the fields in a given section on the form based on the selection of a boolean option (“Two Option” field).

The scenario is useful if, for example, you want to disable data entry in the fields of a section under certain conditions, but you want to still display the disabled fields (rather than change their visibility and hide them). You could reference each field in the section by name and add the .setDisabled(true) method to the end of the control. But this is messy – what happens if later on the fields in the section are changed by adding new ones or removing others?

The script I came up with below handles this pretty nicely. All you need to know is the label of the section you’re concerned with. In this example, I have a boolean field on another section (that’s important – you don’t want to disable this field and then the user can’t change it back!).

//THIS FUNCTION DISABLES ALL THE FIELDS IN THE SECTION LABELED “PRODUCT INFO”
//IF A BOOLEAN OPTION FIELD IN ANOTHER SECTION CALLED new_toggleSectionFlds = TRUE

function DisableSectionAttributes() {
function setFldDisabled(ctrl) {
ctrl.setDisabled(true);
}
function setFldEnabled(ctrl) {
ctrl.setDisabled(false);
}
    var toggleSectionFlds = Xrm.Page.getAttribute(‘new_toggleSectionFlds’).getValue();
var ctrlName = Xrm.Page.ui.controls.get();
for (var i in ctrlName) {
var ctrl = ctrlName[i];
var ctrlSection = ctrl.getParent().getLabel();
if (toggleSectionFlds == true) {
if (ctrlSection == “Product Info”) {
setFldDisabled(ctrl);
}
}
else {
if (ctrlSection == “Product Info”) {
setFldEnabled(ctrl);
}
}
}
}

Put this function in a web resource and then reference it from your form. Call the “DisableSectionAttributes” function in the OnLoad of your form, and in the OnChange of the new_toggleSectionFlds field. Notice that the section name “Product Info” is specified in both the “if” and the “else” part of the function – otherwise you would end up disabling all the other fields on the form!