How to Hide and Show a Tab Based on Field Values on the Account in Dynamics CRM

In this article I will cover how to hide and show a tab based on field values on the account. You may find this useful when you would like to show or hide several fields based on the type of account you are looking at.

In this example I added:

  • A new field to the Account entity to the main account form with two selection values
  • Two custom tabs
  • A dummy field to each tab (but you can add as many fields as needed)

 

New Field

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 1

New Tabs Information

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 2   Hide Show Tabs Scripting Code Microsoft Dynamics CRM 3 Prospect Data

 

Here are the screenshots of my account form after these changes.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 4 Account Information   Hide Show Tabs Scripting Code Microsoft Dynamics CRM 5 Customer Prospect Data

 

Next you need to create a java file that will hide or show the tab based on the value in the Type field. You can create a new text file and change the file extension to ‘.js’. I used the file name ‘Hide and Show Account Tabs.js’.

Open the file in a text editor and add the following code:

function showHideAccountTabs()
{
Type = Xrm.Page.getAttribute(“new_type”);
if (typeof (accountType) != “undefined” && accountType.getValue() != null)
{
if(accountType.getText()==”Customer”)
{
Xrm.Page.ui.tabs.get(“ProspectData”).setVisible(false);
Xrm.Page.ui.tabs.get(“CustomerData”).setVisible(true);
}
else if (accountType.getText()==”Prospect”)
{
Xrm.Page.ui.tabs.get(“ProspectData”).setVisible(true);
Xrm.Page.ui.tabs.get(“CustomerData”).setVisible(false);
}
}
}

Save your code file and add the file as a Web Resource to your Dynamics CRM. This can be done by going to Settings -> Customizations. Select Web Resource then click on New.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 6 Web Resources

 

Fill out the information as shown below and select your script file. Click on Save. Click on Publish. Close the window.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 7 Web Resource New

 

Next you need to add this Web Resource to the main form and configure it to run when the form loads and when the Type field is changed. In the customization area expand Entities. Expand Account. Click on form and open your main account form with the Type field.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 8 Forms

 

Click on Form Properties.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 9 Account Form

 

In the Form Library section add your script file.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 10 Form Properties

 

In the Event Handler area, confirm that Control is set to Form and Event is set to OnLoad and click on Add.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 11 Event Handlers

 

Select your script file and type the name of your function from the script file. Click Ok. Click Ok on the Form Properties window.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 12 Handler Properties

 

Now select the Type field on your account form and click on Change Properties then click on the Events tab at the top. Confirm that the Event is set to OnChange and the Control is name of your field. Click on Add in the Event Handlers section.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 13 Field Properties

 

Select your script file and type in the function name from the script file. Click on Ok. Click Ok on the Field Properties window.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 14 Handler Properties

 

On the account form click on Save. Click on Publish. Close the account form window. Close the customization window.

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 15 Account

 

Go to any account in Dynamics CRM to test the changes. Notice as you change the account Type field the appropriate tab is visible and the other tab is hidden.

 

Prospect

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 16 Company Summary Hide Show Tabs Scripting Code Microsoft Dynamics CRM 17 Prospect Details

 

Customer

 

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 18 Account

Hide Show Tabs Scripting Code Microsoft Dynamics CRM 19 Customer Data Details


Related
Recommended