CRM 2011 Hide tab based on option set value

This question is answered

I am using the following code to hide two tabs and show them depending on the value returned form the option set however I continue to get the error below and can not seem to find where the code is incorrect. The first function is called onload of the form and the second and third functions are called upon the change state of the noted field. (note: I am not a developer nor do I have strong javascript coding experience)

Error:

There was an error with this field's customzed event.

Field: window

Event: onload

Error: The value of the property 'hideTabSection' is null or undefined, not a Function object.

Code:

function hideTabSection() {
     var option = Xrm.Page.getAttribute(“nhs_guaranteetype”).getSelectedOption().text;
     if ((option = null) || (option == "undefined)) {
 Xrm.Page.ui.tabs.get(2).setVisible(false)
                     Xrm.Page.ui.tabs.get(1).setVisible(false)
     }

function setVisibleGuarantees() {
     var option = Xrm.Page.getAttribute(“nhs_guaranteetype”).getSelectedOption().text;
          if (option == "Performance Guarantee")
 {
 Xrm.Page.ui.tabs.get(2).setVisible(true)
                     Xrm.Page.ui.tabs.get(1).setVisible(false)
 }

function setVisibleAllotments() {
     var option = Xrm.Page.getAttribute(“nhs_guaranteetype”).getSelectedOption().text;
          if (option == "Allotments")
 {
 Xrm.Page.ui.tabs.get(1).setVisible(true)
                     Xrm.Page.ui.tabs.get(2).setVisible(false)
 }

Any help would be greatly appreciated

Verified Answer
  • You need to add an if statement to run the process above when guarantee type does not equal NULL, when it equals NULL you hide the tab.

    I can put something for you tomorrow morning when I get to the office and don't have to write from my cellphone.

  • You should end up with something like this...

    function hideTabSection() {
        if (Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption() == null) {
            Xrm.Page.ui.tabs.get(1).setVisible(false);
            Xrm.Page.ui.tabs.get(2).setVisible(false);
        } else {
            var option = Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption().text;
            if (option == "Performance Guarantee") {
                Xrm.Page.ui.tabs.get(2).setVisible(true);
                Xrm.Page.ui.tabs.get(1).setVisible(false);
            } else if (option == "Allotment") {
                Xrm.Page.ui.tabs.get(1).setVisible(true);
                Xrm.Page.ui.tabs.get(2).setVisible(false);
            }
        }
    }
    

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

All Replies
  • Can you give this a try? There were some missing curly braces and quotes.

    function hideTabSection() {
        var option = Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption().text;
        if ((option = null) || (option == "undefined")) {
            Xrm.Page.ui.tabs.get(2).setVisible(false);
            Xrm.Page.ui.tabs.get(1).setVisible(false);
        }
    }
    
    function setVisibleGuarantees() {
         var option = Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption().text;
             if (option == "Performance Guarantee") 
             {
                Xrm.Page.ui.tabs.get(2).setVisible(true);
                Xrm.Page.ui.tabs.get(1).setVisible(false);
             }
    }
    
    function setVisibleAllotments() {
        var option = Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption().text;
        if (option == "Allotments") 
        {
            Xrm.Page.ui.tabs.get(1).setVisible(true);
            Xrm.Page.ui.tabs.get(2).setVisible(false);
        }
     }
    

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Thank you SO much for your help!!

    Closer - Now getting Error Unable to get value of the property 'text': object is null or undefined and the tabs initially do not hide.  However if you clear the onload error you do get the proper function when the option is set in the optionset field. (ie the tabs do hide appropriately). 

    I should also say that after looking at your suggestion I did combine the code into a single function and used it both onload of the form and on change of the optionlist field. I am calling the function through the event handler properties and do not have the "Pass execution context as first parameter checked nor do I have anything listed in the comma seperated list of parameters.

    function hideTabSection() {
        var option = Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption().text;
        if (option == "") {
            Xrm.Page.ui.tabs.get(1).setVisible(false);
            Xrm.Page.ui.tabs.get(2).setVisible(false);
        }
     else if (option == "Performance Guarantee")
             {
                Xrm.Page.ui.tabs.get(2).setVisible(true);
                Xrm.Page.ui.tabs.get(1).setVisible(false);
             }
     else if (option == "Allotment")
        {
            Xrm.Page.ui.tabs.get(1).setVisible(true);
            Xrm.Page.ui.tabs.get(2).setVisible(false);
        }

  • You need to add an if statement to run the process above when guarantee type does not equal NULL, when it equals NULL you hide the tab.

    I can put something for you tomorrow morning when I get to the office and don't have to write from my cellphone.

  • You should end up with something like this...

    function hideTabSection() {
        if (Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption() == null) {
            Xrm.Page.ui.tabs.get(1).setVisible(false);
            Xrm.Page.ui.tabs.get(2).setVisible(false);
        } else {
            var option = Xrm.Page.getAttribute("nhs_guaranteetype").getSelectedOption().text;
            if (option == "Performance Guarantee") {
                Xrm.Page.ui.tabs.get(2).setVisible(true);
                Xrm.Page.ui.tabs.get(1).setVisible(false);
            } else if (option == "Allotment") {
                Xrm.Page.ui.tabs.get(1).setVisible(true);
                Xrm.Page.ui.tabs.get(2).setVisible(false);
            }
        }
    }
    

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • ^ that seems right!

  • Thank you both for such quick and amazing assistance. It worked like a charm.