Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

JavaScript to show/hide section based on option set selection

  • 1.  JavaScript to show/hide section based on option set selection

    Posted Jan 14, 2019 02:16 PM

    I have the following JavaScript to show/hide sections in a tab based on the option set selection and I'm getting three errors when loading the form and when trying to change the option set selection. Can anyone please help me determine where I'm going wrong? I have applied the event as an On Change to the option set field. 

    Thank you very much 

    function HideOrShowSectionBasedOnoptionSetValue(){
     var value = Xrm.Page.getAttribute("new_testrequesttype").getValue();
     if(value!=null){
      if(value == 100000000){
        Xrm.Page.ui.tabs.get("Test Request").sections.get("Lab Test Request").setVisible(true);  //Shows section 1
        Xrm.Page.ui.tabs.get("Test Request").sections.get("Application Test Request").setVisible(false);  //Hides section 2
     
    }
      else if(value == 100000001){
        Xrm.Page.ui.tabs.get("Test Request").sections.get("Lab Test Request").setVisible(false);  //Hides section 1
        Xrm.Page.ui.tabs.get("Test Request").sections.get("Application Test Request").setVisible(true);  //Shows section 2
     }

    On form load: 

    Error 1: 

    Script Error

    One of the scripts for this record has caused an error. For more details, download the log file.
    ReferenceError: 'ShowHideTestTabs' is not defined at eval code (eval code:1:1)

    Error 2: 

    Script Error

    One of the scripts for this record has caused an error. For more details, download the log file.
    TypeError: Unable to get property 'Initialize' of undefined or null reference at eval code (eval code:1:1)

    When the option set selection is changed: 

    Error 3: 

    Script Error

    One of the scripts for this record has caused an error. For more details, download the log file.
    SyntaxError: Expected ';' at RunHandlerInternal 



    ------------------------------
    Martha Paradise
    Nye Lubricants, Inc.
    Fairhaven MA
    ------------------------------
    CRMUG Summit - Post


  • 2.  RE: JavaScript to show/hide section based on option set selection

    GOLD CONTRIBUTOR
    Posted Jan 14, 2019 02:36 PM
    Martha,
    Inside both tabs and sections properties you can find "Name" and "Label". You should use value from "Name" field in your scripts. I believe you used value from "Label" because value in "Name" can't contain spaces.

    ------------------------------
    Andrew Butenko
    ------------------------------

    CRMUG Summit - Post


  • 3.  RE: JavaScript to show/hide section based on option set selection

    GOLD CONTRIBUTOR
    Posted Jan 14, 2019 02:38 PM
    Also it seems that you missed one closing curly bracket - }

    ------------------------------
    Andrew Butenko
    ------------------------------

    CRMUG Summit - Post


  • 4.  RE: JavaScript to show/hide section based on option set selection

    Posted Jan 14, 2019 03:22 PM
    Hi Andrew,

    Thank you very much for taking a look and for your quick reply here and on the D365 Community site. I deleted the tab/sections and started from scratch. The form now loads just fine, progress! Below is my new script and now I'm just getting one error when I try to change the option set selection.

    Error:

    One of the scripts for this record has caused an error. For more details, download the log file.
    ReferenceError: 'HideOrShowSectionBasedOnoptionSetValue' is not defined at eval code (eval code:1:1)

    Script:

    function HideOrShowSectionBasedOnoptionSetValue(){
    var value = Xrm.Page.getAttribute("new_testrequesttype").getValue();
    if(value!=null){
    if(value == 100000000){
    Xrm.Page.ui.tabs.get("Test_Request").sections.get("TestRequest_section_2").setVisible(true);
    Xrm.Page.ui.tabs.get("Test_Request").sections.get("TestRequest_section_3").setVisible(false);

    }
    else if(value == 100000001){
    Xrm.Page.ui.tabs.get("Test_Request").sections.get("TestRequest_section_2").setVisible(false);
    Xrm.Page.ui.tabs.get("Test_Request").sections.get("TestRequest_section_3").setVisible(true);
    }
    }

    Thanks again,

    Martha

    ------------------------------
    Martha Paradise
    Nye Lubricants, Inc.
    Fairhaven MA
    ------------------------------

    CRMUG Summit - Post


  • 5.  RE: JavaScript to show/hide section based on option set selection

    GOLD CONTRIBUTOR
    Posted Jan 14, 2019 03:25 PM
    Martha,
    Your code missing one closing curly bracket - } Try to add it to the end of your code.

    ------------------------------
    Andrew Butenko
    ------------------------------

    CRMUG Summit - Post


  • 6.  RE: JavaScript to show/hide section based on option set selection

    Posted Jan 14, 2019 03:32 PM
    It works!! Thank you so much!!!

    All the best,

    Martha

    ------------------------------
    Martha Paradise
    Nye Lubricants, Inc.
    Fairhaven MA
    ------------------------------

    CRMUG Summit - Post


  • 7.  RE: JavaScript to show/hide section based on option set selection

    GOLD CONTRIBUTOR
    Posted Jan 15, 2019 09:58 AM
    A good site to help check your code is https://beautifier.io/

    ------------------------------
    Ben Edgren
    CRM Applications Manager
    Hufcor, Inc
    Janesville WI
    ------------------------------

    CRMUG Summit - Post


If you've found this thread useful, dive deeper into User Group community content by role