javascripttypescriptdynamics-crmcrm

Hide/show fields based on a Boolean with JavaScript Dynamics CRM


Hi Stack community very new to Dynamics CRM development.

I want to achieve the following:

  1. IF Validation Complete equals "Yes" THEN Show field Validated On && Set Validated On field value to UTC now current date, todays date. ELSE Hide field Validated On.

  2. cr253_validationcomplete is a YES/NO field.

Please advise.

 function showValidationOnChange (executionContext) {
        //Initiated Form Context.
        var formContext = executionContext.getFormContext();
        
        var ValidationComplete = formContext.getAttribute("cr253_validationcomplete").getValue();
      
        if (ValidationComplete === true) {
            //Using SetVisible propertly for hiding field Account Address.
            formContext.getControl("cr253_validatedon").setVisible(true);
            formContext.getControl("cr253_validatedon").setValue(Date.now());
        }
    }

Solution

  • Your code just needs some improvements, in particular the setValue should be used with getAttribute and to get a new date in JavaScript is new Date(), also the else condition is missing, my script has not been tested.

    function showValidationOnChange (executionContext) {
        //Initiated Form Context.
        var formContext = executionContext.getFormContext();
        
        var ValidationComplete = formContext.getAttribute("cr253_validationcomplete").getValue();
      
        if (ValidationComplete === true) {
            //Using SetVisible propertly for hiding field Account Address.
            formContext.getControl("cr253_validatedon").setVisible(true);
            formContext.getAttribute("cr253_validatedon").setValue(new Date());
        } else {
            formContext.getControl("cr253_validatedon").setVisible(false);
            formContext.getAttribute("cr253_validatedon").setValue(null);           
        }
    }