Search code examples
javascriptif-statementdynamics-crm

Why this condition


Could someone explain what these conditions are checking for? I understand it has to do with checking to see if other fields are already disabled, but I I am not completely clear on it. What is referencing the getDisabled method doing specifically?

if (!userHasRoleNeeded) {
            Xrm.Page.ui.controls.forEach(function (control, i) {
                if (control && control.getDisabled && !control.getDisabled()) {
                    control.setDisabled(true);

Solution

  • a && b will not execute b if a is "falsey". control.getDisabled is checking to see if control.getDisabled is "truthy" (if it exists), and if it does, it will execute the method. Without this check, if control.getDisabled did not exist, it would try to execute an undefined method and raise an execution error. Why this is done a choice by the library authors.

    Further reading: https://j11y.io/javascript/truthy-falsey/