locked
How to restrict edit of the form in java script ? RRS feed

  • Question

  • Hi All,

    I want to restrict the editing of the form based on some condition using java script .

    Please help me out how can I do this ?

    Thanks in advance

    Thursday, June 12, 2014 6:22 AM

Answers

  • Hi,

    I recently had the requirement to do similar. Below is the code I used to achieve this result;

    function disableAllFieldsOnForm(fieldsToEnable, subgrids) {
        Xrm.Page.ui.controls.forEach(function (control, index) {
            var controlType = control.getControlType();
            switch (controlType) {
                case "standard":
                case "lookup":
                case "optionset":
                    control.setDisabled(true);
                    break;
            }
        });
    
        subgrids.forEach(function (field, index) {
            Xrm.Page.ui.tabs.get(field).setVisible(false);
        });
    
        fieldsToEnable.forEach(function (field, index) {
            Xrm.Page.getControl(field).setDisabled(false);
        });
    }

    You can call this function if the condition necessary is met.

    I have built it to take 2 array inputs; the first is a list of fields you don't want disabled, and the second is all of the subgrids on the form. My specific requirement included removing the ability to add related records, so the easiest way to do this was to remove visibility from the form.

    Using this method is not the tightest method of controlling this requirement, so I would say to only implement it if you are just looking for a cleaner user experience rather than from a security perspective.

    ~ Atomic Coder

    Thursday, June 12, 2014 7:08 AM
  • Hi,

    Do you want to disable some fields based on some condition? If yes, you can do that using:

    Xrm.Page.getControl("your_field_name").setDisabled(true);


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 6:45 AM

All replies

  • Hi,

    Do you want to disable some fields based on some condition? If yes, you can do that using:

    Xrm.Page.getControl("your_field_name").setDisabled(true);


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 6:45 AM
  • Hi,

    I recently had the requirement to do similar. Below is the code I used to achieve this result;

    function disableAllFieldsOnForm(fieldsToEnable, subgrids) {
        Xrm.Page.ui.controls.forEach(function (control, index) {
            var controlType = control.getControlType();
            switch (controlType) {
                case "standard":
                case "lookup":
                case "optionset":
                    control.setDisabled(true);
                    break;
            }
        });
    
        subgrids.forEach(function (field, index) {
            Xrm.Page.ui.tabs.get(field).setVisible(false);
        });
    
        fieldsToEnable.forEach(function (field, index) {
            Xrm.Page.getControl(field).setDisabled(false);
        });
    }

    You can call this function if the condition necessary is met.

    I have built it to take 2 array inputs; the first is a list of fields you don't want disabled, and the second is all of the subgrids on the form. My specific requirement included removing the ability to add related records, so the easiest way to do this was to remove visibility from the form.

    Using this method is not the tightest method of controlling this requirement, so I would say to only implement it if you are just looking for a cleaner user experience rather than from a security perspective.

    ~ Atomic Coder

    Thursday, June 12, 2014 7:08 AM