Dynamics: using setDisabled/getDisabled may cause your javascript to fail

By | October 16, 2017

I ran into this myself not so long ago, and I just saw the same problem in the community forums, so this is probably something that’s worth being mentioned in a separate blog post.

Not every control can be disabled in Dynamics using setDisabled method

It might not be obvious from the description of setDisabled method in msdn:

https://msdn.microsoft.com/en-us/library/gg334266.aspx

However, it’s easy to prove.

If you add the following script to a web resource, and, then, add “testControls” as an onload event handler to a form, you’ll likely see an alert message once you open a record in Dynamics:

function testControls() {
var controls = Xrm.Page.ui.controls.get();
for (var i in controls) {
var control = controls[i];
if (typeof(control.setDisabled) == ‘undefined’) {
 alert(“There is no setDisabled method for ” + control.getName());
}
}
}

If, somehow, you don’t see an alert.. add a control that cannot be made read-only (add a subgrid, or add an iframe, or a web resource). Just don’t add a regular field for which you’d have this checkbox in the control properties:

 

Once there is that kind of control on the form, you should see an alert similar to the one I have on the screenshot below – that’s for an N:N subgrid:

 

This applies to both setDisabled and getDisabled methods – if there is no setDisabled, there is no getDisabled either.

Depending on the version of Dynamics, when you try using setDisabled with a control that does not have that method, the error you’ll see might be slightly different – it could be “there was an error with the field’s customized event onload”, or it could be “TypeError: control.setDisabled is not a function at”. However, no matter what the version is, unless you are going to use those methods with some specific controls(for which you know that you can use them), you can easily fix this problem by adding a condition like this:

if (typeof(control.setDisabled) != ‘undefined’) control.setDisabled(true);

 

Leave a Reply

Your email address will not be published. Required fields are marked *