setDisable for all fields of a Section in a Crm Form

MauricioJuanes picture MauricioJuanes · Jul 12, 2014 · Viewed 20.1k times · Source

I need to disable a section if a value from other field is true, normally I would do:

function disableSection1(disabledStatus){
    Xrm.Page.getControl("section1field1").setDisabled(disabledStatus);
    Xrm.Page.getControl("section1field2").setDisabled(disabledStatus);
    Xrm.Page.getControl("section1field3").setDisabled(disabledStatus);
    Xrm.Page.getControl("section1field4").setDisabled(disabledStatus);
}

but i have to do this for many sections, so I am looking for a function like this:

function sectionSetDisabled(tabNumber, sectionNumber, disabledStatus){
    //some code..
}

Answer

MauricioJuanes picture MauricioJuanes · Jul 14, 2014

Most answers I have seen you have use the use the sectionLable and do the following comparison: controlIHave.getParent().getLabel()=="name of the section

But after some trials I found I could use Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).controls.get() to get the controls inside the section. That allowed me to use the following function:

function sectionSetDisabled(tabNumber, sectionNumber, disablestatus) {
    var section = Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber);
    var controls = section.controls.get();
    var controlsLenght = controls.length;

    for (var i = 0; i < controlsLenght; i++) {
        controls[i].setDisabled(disablestatus)
    }
}

by using controls[i].getAttribute() you can then get the attributes of a section.

I ended up creating a object that allows me to disable and clear all the fields in a section:

function sectionObject(tabNumber, sectionNumber) {
    var section = Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber);

    this.setDisabled = function (disablestatus) {
        var controls = section.controls.get();
        var controlsLenght = controls.length;
        for (var i = 0; i < controlsLenght; i++) {
            controls[i].setDisabled(disablestatus)
        }
    };

    this.clearFields = function () {
        var controls = section.controls.get();
        var controlsLenght = controls.length;
        for (var i = 0; i < controlsLenght; i++) {
            controls[i].getAttribute().setValue(null);
        }
    };

}

var section=new sectionObject(0,1);
section.setDisabled(true/false);