Converting to CRM 2011 JavaScript: Showing and Hiding form elements

Let’s face it: we all did unsupported customizations with our CRM 4.0 JavaScript in order to present to the user an interface that was contextually relevant. Most of that modification involved the showing and hiding of fields, sections, and tabs through the DOM manipulation by setting the .style property to either show or hide things.

We Dynamics CRM 2011, we have an object model that supports the showing and hiding of:

  • Tabs
  • Sections
  • Fields
  • Navigation items

by using code similar to this:

 

Xrm.Page.getControl("name").setVisible(false);
Xrm.Page.ui.navigation.items.get("navActivities").setVisible(false);

 

Note: There is also a corresponding .getVisible method that gets the visibility state of an item.

So, when you are upgrading your code, you need to convert any instance of:

// Hide fields
crmForm.all.fieldname_c.style.visibility="hidden";
crmForm.all.new_fieldname_c.style.display = 'none'; 
crmForm.all.new_fieldname_d.style.display = 'none'; 

// Show fields
crmForm.all.fieldname_c.style.visibility="visible";
crmForm.all.new_fieldname_c.style.display = 'inline'; 
crmForm.all.new_fieldname_d.style.display = 'inline'; 

to:

 

// Hide fields
Xrm.Page.getControl("fieldname_c").setVisible(false);
Xrm.Page.getControl("new_fieldname_c").setVisible(false); 
Xrm.Page.getControl("new_fieldname_d").setVisible(false); 

// Show fields
Xrm.Page.getControl("fieldname_c").setVisible(true);
Xrm.Page.getControl("new_fieldname_c").setVisible(true); 
Xrm.Page.getControl("new_fieldname_d").setVisible(true);

This is also something that the CRM Migration Assistant handles quite nicely.

 

One final note:

In the samples above, you will see the use of field names that end with "_c" and "_d."  These are the field label and field edit box respectively.

When converting your code, you will need to delete one of the lines of code, like the one that shows/hides the "_d" field, and remove the "_c" from the field name so that you code looks like this:

 

Xrm.Page.getControl("new_fieldname").setVisible(true);

We do this because CRM would throw an error if you attempted to hide the "partial" field.

Leave a Reply