When working with CRM, you often want to enable (set to read/write) or disable (set to read / only) selected fields, sections, tabs and the whole form depending on your business logic. This is can be derived from studying the SDK and creating som code, but here are my easy-to-use support functions to complement the base functions.
1) Enable / Disable a field
Xrm.Page.getControl("fieldname").setDisabled(false); // true = R/W, false = R/O
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