Sunday, 24 June 2018

How to Disabling controls in a section in Dynamic CRM using JavaScript

How to enable/disable a section in Dynamics CRM

Description:
In this example we explain that how to enable/disable controls in a section in Dynamic CRM using JavaScript. In previous example we already explain that how to enable/disable control or field using JavaScript in Dynamic CRM. but now in this post we will explain that how to disabling controls in a section using JavaScript.

We already know that we can’t disable a section using “SetDisable(false)” same like field because section is the container of the controls so we can’t disable them.so to overcome this problem we have to create another JavaScript code like below.
Functions:

function disableSectionFields(section) {

            if (section) {

                Xrm.Page.ui.controls.forEach(function (control, index) {

                    if (control && doesControlHaveAttribute(control)) {

                        if (control.getParent() === section) {

                            control.setDisabled(true);

                        }

                    }

                });

            }

        }
        // Validate if the control is not  IFrame/webresource/subgrid as we can’t disable them

        function doesControlHaveAttribute(control) {

            var controlType = control.getControlType();

            return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";

        }
        // Get the Section object by Tab name & Section name

        function getSection(tabName, sectionName) {

            var tab = Xrm.Page.ui.tabs.get(tabName);

            if (tab) {

                return tab.sections.get(sectionName);

            }

            return null;

        }
        var mySection = getSection("tab_name", "section_name");

        if (mySection) {

            disableSectionFields(mySection);

        }


0 comments:

Post a Comment