Dynamics CRM Javascript making form read only -TypeError: control.getDisabled is not a function

Attempting to make the price list form and price items form read only and using the below java script however keep getting the following error; One of the scripts for this record has caused an error. For more details, download the log file. TypeError: control.getDisabled is not a function at makeFieldsReadOnly

Using Dynamics CRM 2016 and this JS runs on load of the form

function onLoad() {
    var formType = Xrm.Page.ui.getFormType();

    if (formType == 2)
        makeFieldsReadOnly(); }


function makeFieldsReadOnly() {
    var controls = Xrm.Page.ui.controls.get();
    for (var i in controls) {
        var control = controls[i];
        if (!control.getDisabled()) {
            control.setDisabled(true);
       }
    } }

Any suggestions on why this may be failing , working fine in CRM 2011?

Answer

Not all the controls you are iterating over can be disabled and thus the code is failing. It throws an error when it tries to execute getDisabled on a control, which does not have this function declared.

This can be fixed by checking if the necessary functions exist on the control first:

function onLoad() {
    var formType = Xrm.Page.ui.getFormType();

    if (formType == 2)
        makeFieldsReadOnly(); }


function makeFieldsReadOnly() {
    var controls = Xrm.Page.ui.controls.get();
    for (var i in controls) {
        var control = controls[i];
        if (control.getDisabled && control.setDisabled && !control.getDisabled()) {
            control.setDisabled(true);
       }
    } 
}

Leave a Reply

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