Follow Us:
There are SDK and web examples of how to disable or hide an entire tab on a form in Microsoft 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!
very useful, easy and powerful piece of code. Excellent Job!
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.