web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

CRM2011 – Disable / enable fields, sections, tabs and the whole form

Alessandro Graps Profile Picture Alessandro Graps 2,664

Today the problem is about enable and disable fields on form in CRM. 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.

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) Disabling all Fields on a Form
Add this code to your web resource:

function doesControlHaveAttribute(control) {
    var controlType = control.getControlType();
    return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
}


function disableFormFields(onOff) {
    Xrm.Page.ui.controls.forEach(function (control, index) {
        if (doesControlHaveAttribute(control)) {
            control.setDisabled(onOff);
        }
    });
}

Call the function:
Generally, you will have another function that is triggered on-load that will determine if the form should be disabled. It may check a picklist value or it may check the current user’s role.

function setupForm(){
    if (Xrm.Page.ui.getFormType() == 2 &&
        Xrm.Page.getAttribute("incidentstagecode").getValue() != null &&
        Xrm.Page.getAttribute("incidentstagecode").getValue() == "200001") 
    {
           disableFormFields(true);
    }
}

- See more at: http://blog.avtex.com/2011/04/01/disabling-all-fields-on-a-form-in-crm-2011/#sthash.nfN8z64a.dpuf



This was originally posted here.

Comments

*This post is locked for comments