Thursday, July 17, 2014

Disable / Enable form fields through JavaScript

There are many scenarios where we need to disable all fields/controls on an Entity form. Here I am showing different ways to handle it through JavaScript.

function disableFields() {
var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes) {
Xrm.Page.getControl(attributes[i].getName()).setDisabled(true);
Xrm.Page.getControl(“statuscode”).setDisabled(false);
}
}
function enableFields(){
var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes){
Xrm.Page.getControl(attributes[i].getName()).setDisabled(false);
}
}

  • Enable / Disable a field

Xrm.Page.getControl(“fieldname”).setDisabled(false);

  • Enable / Disable a Section

function sectiondisable (sectionname, disablestatus)
{
var ctrlName = Xrm.Page.ui.controls.get();
for(var i in ctrlName) {
var ctrl = ctrlName[i];
var ctrlSection = ctrl.getParent().getName();
if (ctrlSection == sectionname) {
ctrl.setDisabled(disablestatus);
}
}

  • Enable / Disable a Tab

function tabdisable (tabname, disablestatus)
{
          var tab = Xrm.Page.ui.tabs.get(tabname);
 if (tab == null)
alert("Error: The tab: " + tabname + " is not on the form");
 else {
     var tabsections =  tab.sections.get();
     for (var i in tabsections) {
     var secname = tabsections[i].getName();
     sectiondisable(secname, disablestatus);
              }
                 }
}


No comments:

Post a Comment