Hide A Tab based upon Account Type

After seeing some of our top partners such as Invoke and ePartners demo this functionality, I was dying to see how they did it.  Then Simon Hutson posted on his blog a script to hide a tab based upon an account being a partner or a customer.  Well, after seeing both scripts, both where awesome, but both had their drawbacks. Simon used OnLoad and ePartners used OnChange. What are the pros and cons?

Before we start with the pro's and con's, lets go through the three options available to you. The three scripting events you can have events fire on are, OnLoad, OnChange and OnSave.

   Onload:

  • The Onload event occurs after the form has loaded; it cannot prevent the window from loading. The OnLoad event is valuable because it can apply logic about how the form should be displayed and can be used to set certain fields to read-only or business required.

OnChange:

  • The OnChange event is fired when the data in a form field has changed and focus is lost. The Microsoft CRM 3.0 application processes the OnChange event first and the data in the field is validated again following the execution of your OnChange event code.

OnSave:

  • This event does not correspond to the standard OnSave event exposed by DHTML. It is fired when a user presses the Save or Save and Close button on the form. The event is fired whether the data in the form has been modified or not.
  • Validating data is a common reason to use the OnSave event. The OnSave event can cancel the save operation before it is sent back to the server. To cancel the save operation, the script should return false as shown in the following code.

So now onto Pros and Cons... :-)

With OnLoad, when the form is loaded the conditions are evaluated and the action is carried out. So if a account goes from Prospect to Customer, the tab would not be hidden until the form is reloaded. So that would require the user to save and close the window and then open back up to get the tab to show up as hidden. So if an account never moves, then this method alone would do the job for you.

With OnChange, when you change the value the condition is evaluated and the action is carried out. Once you change somebody to a supplier that tab would only be hidden while that form was open. So if you change the value, it will hide the tab, but if you save and open the form back up, the tab is back.... So OnChange works to give the user immediate feedback.

So with a little bit of time and some help from Paul Reitmeyer from Invoke, we created two scripts that handles both scenarios. How you ask? But using both the OnLoad and OnChange Events, we can play to the strengths in each of them.

So we are going to use a new tool today. Here are the steps required to hide tabs when a account opens and the picklist changes. :-)

 
  • Run the File
  • Open Internet Explorer and right click just below the address bar and unselect Lock the Toolbars. Select the Option that says “Developer Toolbar”
  • Click the option that says Lock the Toolbars.

 
  • Close Internet Explorer

 
  • Start Microsoft CRM in Internet Explorer.
 
  • Select the Accounts Area
  • Open an Account up
  • Hit the F11 Button to bring Microsoft CRM out of application mode.
  • Click on View DOM
  • Click in the DOM Explorer (At the bottom of the screen) And under the Find Menu, select “Select Element by Click.”)

  • Now click on the Details tab.
  • You will now see this in the center of the DOM Explorer.
    • The id name is the one that you want. It is what we will use to identify the tab to hide.
  • Close the Account Screen
 
  • Now go to Settings / Customizations and select the Account Entity

 
  • Open the Form
 
  • Select Form Properties
 
  • Click on Events
 
  • Click on OnLoad
 
  • Paste the following into the area and make sure the Event Enabled box is checked

           if( window.document.forms( "crmForm" ).elements( "customertypecode" ).SelectedText == "Supplier" || window.document.forms( "crmForm" ).elements( "customertypecode" ).SelectedText == "Customer")

           {          

             crmForm.all.tab1Tab.style.display="none";     

            }

  • Click Ok and Close the Form Properties.
 
  • Click on the Relationship Type Field. Open the properties of the Relationship Type and click on Events.
 
  • Click on OnChange
 
  • Paste the following into the area and make sure the Event Enabled box is checked.

if (crmForm.all.customertypecode.SelectedText == "Customer" || crmForm.all.customertypecode.SelectedText == "Supplier")

{

    crmForm.all.tab1Tab.style.display="none";

}

 

  • Save and close the form.
 
  • Save and Close the Account Entity
 
  • Publish the Account Entity
 
  • The Details Tab will now disappear if the relationship type is either Customer or Supplier...
 

An Account with the Relationship Type of Investor

The Same Account with the Relationship Type of Customer

These scripts could use some further enhancements. (I see one little hole already... Change it from a Customer to a Prospect... The form doesn't refresh... So the script needs a little more work...) What would you like to see in some future script examples?