Friday, 24 May 2013

How disable form , tab , filed using java script in Mscrm2011


Category Archives: Ms Crm 2011 JavaScript Events

Disable / Enable fields, sections, tabs and the whole Form in MS CRM 2011

Disable/Enable fields, sections, tabs and the whole Form in MS CRM 2011
When working with the MS CRM form , Your requirement will be to enable (set to read/write) or disable (set to read / only) selected fields, sections, tabs and the Whole form.
Please have a glance below code for these functionalities to work out.
1)     Enable / Disable a field
Xrm.Page.getControl(“fieldname”).setDisabled(false); 
2)    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);
}
}
}  // sectiondisable
3)    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);
     }
  }
}   // tabdisable

4)    Enable / Disable a Form
function formdisable(disablestatus)
{
    var allAttributes = Xrm.Page.data.entity.attributes.get();
    for (var i in allAttributes) {
           var myattribute = Xrm.Page.data.entity.attributes.get(allAttributes[i].getName());
           var myname = myattribute.getName();          
           Xrm.Page.getControl(myname).setDisabled(disablestatus); 
    }
} // formdisable

5)     Enable / Disable All Controls in the TAB
function DisableAllControlsInTab(tabControlNo) 
{  
var tabControl = Xrm.Page.ui.tabs.get(tabControlNo);
    if (tabControl != null) 
{    
      Xrm.Page.ui.controls.forEach
( 
     function (control, index) 
{          
if (control.getParent().getParent() == tabControl && control.getControlType() != "subgrid") 
{              control.setDisabled(true);  
        }      
});
      } 
 }
function EnableAllControlsInTab(tabControlNo)
 {      
var tabControl = Xrm.Page.ui.tabs.get(tabControlNo);      
if (tabControl != null) 
{         
 Xrm.Page.ui.controls.forEach
(      
function (control, index) 
{          
if (control.getParent().getParent() == tabControl && control.getControlType() != "subgrid") 
{              
control.setDisabled(false);  
        }  
    });  
    } 
 }