Pages

Thursday 13 April 2017

MS CRM - Better way of Hiding or Disabling a field in Form - #JSTip3


Hello All,

Lets go through a quick development tip regarding hiding or disabling an attribute using JavaScript,

To hide or disable a field in CRM, its very common to use Xrm.Page.getControl. Though the method is very handy it has its own restrictions.

Xrm.Page.getControl - Not the best way
This function will return only the first occurrence of the specific attribute in the form.

Consider we have statecode attribute at multiple area's of a form, In this scenario getcontrol can handle only the first occurrence of this attribute, which affects the robustness of the solution.


Xrm.Page.getAttribute("AttributeName").controls
Instead of using getControl we can use getAttribute.controls to hide all the fields related to a particular attribute.

Here we select the attribute, then we loop through all the controls available for that attribute and we are hiding them.

Sample code snippets are as follows,

Usual Way:
Xrm.Page.getControl("statecode").setVisible(false);

Best way:
Xrm.Page.getAttribute("statecode").controls.forEach(
        function (control) {
            control.setVisible(false);
        });









1 comment:

  1. the hide is a bit pointless as it retains the space on the Form. Is there a way (apart from creating many sections) to hide the space on the Form

    ReplyDelete