locked
CRM 2011 - JavaScript Requirement RRS feed

  • Question

  • Hi,

    I'm having some difficulty creating some JavaScript, I require a Navigation item to hide based on a optionset value onload, I can get this to work for existing records but when I create a record I get the below error: my code is also below - Any assistance would be greatly appreciated, I'm still a novice with JS when implementing with a CRM solution - I suspect that the issue lies with the optionset value on create is a unassignmed valuee (null) so some extra code is required.

    function hidenavsites()

        if(Xrm.Page.getAttribute("new_value").getSelectedOption().text =="1")

     {
     Xrm.Page.ui.navigation.items.forEach(function (item, index)
        {
            var itemLabel = item.getLabel();
      
      if (itemLabel =="Company")  {
                item.setVisible(false);
      }
        })
     }     
    }

    Tuesday, October 8, 2013 2:58 PM

All replies

  • Try this when looking for the option set data:

    //Gets the text value displayed to the user
    Xrm.Page.data.entity.attributes.get("new_value").getValue()
    //Gets the integer value stored by the system
    Xrm.Page.data.entity.attributes.get("new_value").getText()


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Tuesday, October 8, 2013 3:02 PM
    Moderator
  • Thanks for the response Jason, I've applied your code Xrm.Page.data.entity.attributes.get("new_value").getValue() to my existing code, and it fires successfully and hides the navigation item, however it also hides the navigation item when the optionset/picklist equals 2,3 and 4. Its only specific to "1" that I need the nav item to hide. Apologies if I didnt outline this in my original post.

    function hidenavsites()

        Xrm.Page.data.entity.attributes.get("new_value").getValue("1")
      {
     Xrm.Page.ui.navigation.items.forEach(function (item, index)
        {
            var itemLabel = item.getLabel();
      
      if (itemLabel =="1")  {
                item.setVisible(false);
      }
        })
     }
         
    }

    Tuesday, October 8, 2013 3:28 PM
  • You need at add an "if" condition to check the value of the option set and only fire when the value equals 1

    Not tested...

    function hidenavsites() {
        if (Xrm.Page.data.entity.attributes.get("new_value").getValue() == 1) {
            Xrm.Page.ui.navigation.items.forEach(function (item, index) {
                var itemLabel = item.getLabel();
                if (itemLabel == "1") {
                    item.setVisible(false);
                }
            })
        }
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by Faisal Fiaz Wednesday, October 9, 2013 8:05 AM
    Tuesday, October 8, 2013 3:47 PM
    Moderator