locked
CRM 2011 - Hiding Navigation Items JavaScript RRS feed

  • Question

  • Hello,

    I have a requirement where I need some javascript where based on a option set value, a navigation item is hidden. For example my option set is called " Company or Site" and has 2 values "Company" OR "Site". When the optionset = Site - I need the Navigation item called "Sites" not to be visible to the end user. I have the below code which executes successfully when you open up an existing company or site, however when you create a new record it throws an exception (I suspect because the default value for the optionset is set to unassigned). Any assistance would be greatly appreciated as its driving me insane :(

    Thanks

    function hide()
     if(Xrm.Page.data.entity.attributes.get("cvs_companyorsite").getSelectedOption().text =="Site")
        Xrm.Page.ui.navigation.items.forEach(function (item, index)
        {
            var itemLabel = item.getLabel();
      
            if (itemLabel == "Sites") {
                item.setVisible(false);
            }
        })
    }


    • Edited by samrr1875 Wednesday, October 9, 2013 10:11 AM error in initial uploaded code
    Wednesday, October 9, 2013 10:08 AM

Answers

  • try this :

    function hide() {
        if (Xrm.Page.getAttribute("cvs_companyorsite").getSelectedOption() != null) {
            if (Xrm.Page.getAttribute("cvs_companyorsite").getSelectedOption().text == "Site")
                Xrm.Page.ui.navigation.items.forEach(function (item, index) {
                    var itemLabel = item.getLabel();
    
                    if (itemLabel == "Sites") {
                        item.setVisible(false);
                    }
                })
        }
    }
    


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by samrr1875 Wednesday, October 9, 2013 2:52 PM
    Wednesday, October 9, 2013 2:42 PM

All replies

  • Please try: 

     

    if( Xrm.Page.getAttribute("cvs_companyorsite").getSelectedOption().getText() =="Site") 



    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi


    Wednesday, October 9, 2013 10:24 AM
  • Hi Imran,

    I've just executed the below as requested and unfortunately it still fires an exception when creating a record where the optionset has a unassigned value.

    function hide()
    {   if( Xrm.Page.getAttribute("cvs_companyorsite").getSelectedOption().getText() =="Site") 
        Xrm.Page.ui.navigation.items.forEach(function (item, index)
        {
            var itemLabel = item.getLabel();
      
            if (itemLabel == "Sites") {
                item.setVisible(false);
            }
        })
    }

    Regards

    Wednesday, October 9, 2013 10:32 AM
  • Hi,

    var a =Xrm.Page.data.entity.attributes.get("cvs_companyorsite").getSelectedOption().text ;

     if(a=="Site") 

    Try this one get the option set text in a variable and then set the condition.

    I hope it will work.

    Thanks

    ------------------------

    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, Sorde

    Wednesday, October 9, 2013 10:40 AM
  • Hi,

    Can you share the exception details please?


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    Wednesday, October 9, 2013 11:03 AM
  • This only occurs when I create a new record - existing records there is no exception

    Wednesday, October 9, 2013 11:15 AM
  • try this :

    function hide() {
        if (Xrm.Page.getAttribute("cvs_companyorsite").getSelectedOption() != null) {
            if (Xrm.Page.getAttribute("cvs_companyorsite").getSelectedOption().text == "Site")
                Xrm.Page.ui.navigation.items.forEach(function (item, index) {
                    var itemLabel = item.getLabel();
    
                    if (itemLabel == "Sites") {
                        item.setVisible(false);
                    }
                })
        }
    }
    


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by samrr1875 Wednesday, October 9, 2013 2:52 PM
    Wednesday, October 9, 2013 2:42 PM
  • Thank you Vikram, that works perfectly.

    Wednesday, October 9, 2013 2:52 PM