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..
}
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);
function TabObject(tabName, DisableStatus) {
var sectionName = Xrm.Page.ui.tabs.get(tabName).sections.get();
for (var i in sectionName) {
var controls = sectionName[i].controls.get();
var controlsLenght = controls.length;
for (var i = 0; i < controlsLenght; i++) {
controls[i].setDisabled(DisableStatus);
}
}
}
In CRM 2013 (and later), you can use the forEach
iterator. This essentially allows the functionality in a one-liner.
/* Parameters:
* tabNumber = Tab Name/Id assigned in the form editor.
* sectionNumber = Section Name/Id assigned in the form editor.
*/
function sectionSetDisabled(tabNumber, sectionNumber, disabledStatus) {
// Pull the tab, then section (within the tab) and create an iterator.
Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).controls.forEach(
// Delegate to set the status of all controls within the section.
function (control, index) {
control.setDisabled(disabledStatus);
});
}
Check following article that provides code - http://blogs.msdn.com/b/crm/archive/2011/09/29/disable-all-fields-in-a-section-based-on-the-value-of-another-field-crm-2011.aspx