locked
Disabled fields not saving to database RRS feed

  • Question

  • We written code (below) to disable all fields so they cannot be changed as part of an approval process.  The code changes all the fields to disabled, but when you save/close the record and reopen, all the fields are editable.  Since there are 50+ fields, is there a way to save all the fields to the database?

    //MakeFieldsReadOnly, 
    function AllReadOnly_onchange()
    {
    var RequestApproval = Xrm.Page.getAttribute("synact_requestapproval");
    var ApprovalStatus = Xrm.Page.ui.controls.get("synact_approvalstatus");
    var ApprovalDate = Xrm.Page.ui.controls.get("synact_approvaldate");
    var controls = Xrm.Page.ui.controls.get();
    for (var i in controls)
       {
          var control = controls[i];
    if (!control.getDisabled() && RequestApproval.getValue() == 1)
          {
          control.setDisabled(true);
          ApprovalStatus.setDisabled(false);
          ApprovalDate.setDisabled(false);

          }
       }
    }


    Ken Compter

    Thursday, August 15, 2013 6:15 PM

Answers

  • Modifying the control with JavaScript is only client side, and so when the form reloads, the default controls will be displayed again.

    In other words, if you disable a field and the save the form, when it reloads the field will be enabled again.

    To avoid this you simply need to run the same JavaScript onload as well, so that whenever the form is reloaded, the code is fired and disables the fields if needed.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    • Marked as answer by kcompter Friday, August 16, 2013 1:34 PM
    Friday, August 16, 2013 7:09 AM

All replies

  • Modifying the control with JavaScript is only client side, and so when the form reloads, the default controls will be displayed again.

    In other words, if you disable a field and the save the form, when it reloads the field will be enabled again.

    To avoid this you simply need to run the same JavaScript onload as well, so that whenever the form is reloaded, the code is fired and disables the fields if needed.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    • Marked as answer by kcompter Friday, August 16, 2013 1:34 PM
    Friday, August 16, 2013 7:09 AM
  • Just to add to Paul suggestion. You are disabling fields on change of a field. Use the same field criteria onlaod also.

    Regards Faisal

    Friday, August 16, 2013 7:38 AM
  • That worked!  Great to understand the "why" it is actually happening. 

    Ken Compter

    Friday, August 16, 2013 1:33 PM
  • Just a further note on this type of situation.  We were able to disable/enable fields, but sometimes the data was not being saved.  So we added Xrm.Page.data.entity.save(); to the code before disabling fields.  Saves the users from losing data entered before clicking on the Request Approval checkbox.

    Ken Compter

    Monday, August 19, 2013 6:19 PM