Thursday, January 24, 2013

MSCRM 2011: Displaying a Hidden Field Depending on a Check Box Value


A Client recently asked “I’d like to only show an Option set type field if the value of a Check box is Yes”.
This is possible by adding some java script on the On_change of the check box field.

Following is a list of tasks that need to be performed:

Task 1Create a new JavaScript Library that will include the function to hide/show the Option Set field.

  • Go to Settings, Customizations, Customize the System.
  • Scroll down to Web Resources, Click New from the toolbar.
  •  Enter the following Information in the New Web Resource Dialog:
    • Name= “HideOptionSetonChoicefromCheckBox”
    • Display Name= “Hide Option Set on Choice From Check Box”
    •  Language = “English”
    • Type = “Script (Jscript)”
  • Click the Text Editor button
  • Paste the following script in to the source dialog and make the changes described in the Note comments.
function HideShowOptionSet ()
{
// Note: an_Checkboxname should be replaced with the Name of the field you are examining

// Note: an_OptionSetname should be replaced with the Name of the OptionSet field you are hiding
if (Xrm.Page.getAttribute("an_Checkboxname").getValue() == false)
{

   Xrm.Page.getControl( "an_OptionSetname").setVisible(false);
}

else
{

   Xrm.Page.getControl("an_OptionSetname").setVisible(true);
}

} // End of Function 
  • Click OK
  • Click Save
  • Click Publish
  • Click Save and Close

Task 2 -  Attach JScript function to onChange event handler

Next we need to setup the event handlers on the form being modified.
  • Click on the Entities to expand.
  • Select the Entity where the function will be called from and then click OK.
  • Expand the Entity, choose Form and Information form type Main.
  • Now that you are in the form editor we need to attach the event handler to the Form onChange event.
  • Go to the field that will be changed and double-click. This will bring up the field properties dialog.
  • Go to the Events tab and choose to Add a Forms Library
  • The Web Resource Lookup will be displayed, choose the Web Resource added above and Click OK.
  • In the Event Handler section Click Add.
  • In the Handler Properties dialog do the following: Function =  HideShowOptionSet
  • Check Enabled, Check Pass execution context as first parameter
  • Click OK, to close the Handler Properties dialog
  • Click OK, to close the Field Properties dialog
  • Click Save and then Publish form interface.

Task 3 – Test your Changes.