1
votes

We're on CRM 2016 and I have a requirement to disable all fields on the Contact form when a certain condition is met. This is the code that I use

var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes) {
    var myattribute = Xrm.Page.data.entity.attributes.get(attributes[i].getName());
    var myname = myattribute.getName();
    if (Xrm.Page.getControl(myname) != null) {
        //alert(myname);
        Xrm.Page.getControl(myname).setDisabled(true);
    }
}

The code works perfectly except for some reason I cannot ever get to the fields on the header. Somehow getControl always returns null for the header fields. All fields on the header are not locked, but the footer itself is locked by default and I'm unable to unlock it.

Is it possible to disable header fields in CRM forms? Do I need to find a way to unlock the header on the form? I tried disabling the field itself with below code but no luck.

Xrm.Page.getControl("mycustomfield").setDisabled(true);
4

4 Answers

3
votes

I have to add "header_" into my field names in order to make it work

Xrm.Page.getControl("header_mycustomfield").setDisabled(true);
0
votes

Like answered in another SO thread, you can disable all the controls in header in single shot without hard coding the names.

To optimize your code in question for disabling form controls entirely, use this:

Xrm.Page.ui.controls.forEach(function (control) {
            if (control.setDisabled) {
                control.setDisabled(false);
            }
        });
0
votes
// Tested On CRM v8.2

// lock a header control
Xrm.Page.getControl("header_statuscode").setDisabled(true);

// this won't work if statusreason is hosted in the header section
Xrm.Page.getControl("statuscode").setDisabled(true);

// lock all controls on form, including header controls
Xrm.Page.ui.controls.forEach(function (control) {
    if (control.setDisabled) {
        control.setDisabled(true);
    }
});
0
votes

Using formContext you can also disable it.

 if (formContext.getControl("accountid").getAttribute()!= null)  
     formContext.getControl("accountid").setDisabled(true);