locked
How to set a form to be read only after saving it? RRS feed

  • Question

  • I want to make a form to be read only and can't change anything after click the save button. How to do it ?

    One day a small demo!

    Thursday, February 21, 2013 4:38 AM

Answers

  • You could use JavaScript similar to this (you would have to determine any conditions in the form's OnLoad event to trigger the fields becoming disabled).

    function doesControlHaveAttribute(control) {
        var controlType = control.getControlType();
        return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
    }
    
    function disableFormFields(onOff) {
        Xrm.Page.ui.controls.forEach(function (control, index) {
            if (doesControlHaveAttribute(control)) {
                control.setDisabled(onOff);
            }
        });
    }
    
    function onLoad() {
        if (Xrm.Page.ui.getFormType() == 2) {
            if (Xrm.Page.getAttribute("name").getValue() != null) {
                disableFormFields(true);
            }
        }
    }

    Courtesy of this blog.


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, February 21, 2013 5:03 AM
    Moderator
  • function disableFormFields(onOff) {

        Xrm.Page.ui.controls.forEach(function (control, index) {
            if (doesControlHaveAttribute(control)) {
                control.setDisabled(onOff);
            }
        });
    }

    function doesControlHaveAttribute(control) {
        var controlType = control.getControlType();
        return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
    }


    If the answer helped you, remember to mark it as answer.

    Thursday, February 21, 2013 6:02 AM
    Moderator

All replies

  • You could use JavaScript similar to this (you would have to determine any conditions in the form's OnLoad event to trigger the fields becoming disabled).

    function doesControlHaveAttribute(control) {
        var controlType = control.getControlType();
        return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
    }
    
    function disableFormFields(onOff) {
        Xrm.Page.ui.controls.forEach(function (control, index) {
            if (doesControlHaveAttribute(control)) {
                control.setDisabled(onOff);
            }
        });
    }
    
    function onLoad() {
        if (Xrm.Page.ui.getFormType() == 2) {
            if (Xrm.Page.getAttribute("name").getValue() != null) {
                disableFormFields(true);
            }
        }
    }

    Courtesy of this blog.


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, February 21, 2013 5:03 AM
    Moderator
  • Thanks for your reply. But your code seems doesn't work.  Is there a way to set the attribute of a form to disable the form instead of setting each control ?

    I feel there is a way like this.


    One day a small demo!

    Thursday, February 21, 2013 5:50 AM
  • function disableFormFields(onOff) {

        Xrm.Page.ui.controls.forEach(function (control, index) {
            if (doesControlHaveAttribute(control)) {
                control.setDisabled(onOff);
            }
        });
    }

    function doesControlHaveAttribute(control) {
        var controlType = control.getControlType();
        return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
    }


    If the answer helped you, remember to mark it as answer.

    Thursday, February 21, 2013 6:02 AM
    Moderator
  • You could also use the code below for disabling a single field:

     Xrm.Page.ui.controls.get("fieldname").setDisabled(true);


    If the answer helped you, remember to mark it as answer.

    Thursday, February 21, 2013 6:10 AM
    Moderator
  •  

    Thanks for your reply.

    Must I use this way to loop each control and set the attribute to disabled? I am using the dynamic CRM 2011, is there an easy way to set the attribute of the form and all controls in the form are disabled?


    One day a small demo!

    Thursday, February 21, 2013 7:04 AM
  • As JLattimer and I mentioned above, to disable a form on save or load, you have to use those scripts. To disable a field solely, use the second script as I said.

    If the answer helped you, remember to mark it as answer.

    Thursday, February 21, 2013 7:44 AM
    Moderator
  • try this may helpfull to you!!

    function MakeFieldsReadOnly ()

    {

    var type=Xrm.Page.getAttribute().getFormType();

    if(type==2)

    {

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

    }}
    }


    ms crm

    Thursday, February 21, 2013 7:48 AM