JavaScript basics: Conditionally hide sections and tabs
I recently started looking into javascript, with the purpose of expanding my customization toolbelt for Dynamics CRM, and I wanted to share some of my small customizations here for others to use (primary for myself to use, as I tend to forget quickly).
One of the first things I wanted to get down, was the ability to show/hide sections and tabs on a form. We can already show/hide fields in CRM using a business rule – but it gets a bit tedious if I have to hide more than 4-5 fields. The standard business rule editor…doesnt really have a well designed UI – in my opinion.
First, how to conditionally hide tabs:
function showHideTab()
{
var type = Xrm.Page.getAttribute("FIELD").getValue();
switch(type) {
case 100000000: // If FIELD value is 100000000, then hide the tab
Xrm.Page.ui.tabs.get("TAB NAME").setVisible(false);
break;
default:
// Shows tab
Xrm.Page.ui.tabs.get("TAB NAME").setVisible(true);
}
}
I use a switch statement rather than a if/else/then. But thats just personal taste.
For hiding a section – its basically the same code, but you add a small piece of code to each of the get statements:
function showHideSection()
{
var type = Xrm.Page.getAttribute("FIELD").getValue();
switch(type) {
case 1: // If FIELD value is 1, then hide the section
// Hides section
Xrm.Page.ui.tabs.get("TAB NAME").sections.get("SECTION NAME").setVisible(false);
break;
default:
// Shows section
Xrm.Page.ui.tabs.get("TAB NAME").sections.get("SECTION NAME").setVisible(true);
}
}
Thats it for now. I will slowly get more java script basics in here, as I learn more.