Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

(0) ShareShare
ReportReport
Posted on by 175

Hi all, 

Looking for help on some JScript.  Basically, I want to Show/Hide 4 sections on a form based on a field called 'Deal Stage' certain values:

Show/Hide Current LOI Document Attachments, Current Contract Document Attachments, Current Green Folder Document Attachments, Current Closing Memo Document Attachment Sections based on the corresponding Deal Stage OS values. 
Deal Stage = Pipeline LOI (531,180,000), Show Current LOI Document Attachments Section, else hide 
Deal Stage = Pipeline Contact (531,180,001), Show Current Contract Document Attachments Section, else hide 
Deal Stage = Not CIC Approved (531,180,002), Show Current GF Document Attachments Section, else hide 
Deal Stage = Closing (531,180,004), Show Current Closing Memo Document Attachments Section, else hide

Here is current code that I have that is throwing a "Web Resource Method does not exist" error onLoad/onChange of OS.  Hoping it is syntax or something simple.  Thanks in advance!  

function ShowHideDocAttachmentSections(ExecutionContext) {

var dealStage = formContext.getAttribute("lennar_dealstage").getValue();

var formContext = context.getFormContext();

}

{

var docTab = formContext.ui.tabs.get("tab_12");

}

if (dealStage) != (531, 180, 000) {

formContext.ui.sections.get("tab_12_section_6").setVisible(true);

}

else {

formContext.ui.sections.get("tab_12_section_6").setVisible(false);

}

if (dealStage) != (531, 180, 001) {

formContext.ui.sections.get("tab_12_section_7").setVisible(true);

}

else {

formContext.ui.sections.get("tab_12_section_7").setVisible(false);

}

if (dealStage) != (531, 180, 002) {

formContext.ui.sections.get("tab_12_section_8").setVisible(true);

}

else {

formContext.ui.sections.get("tab_12_section_8").setVisible(false);

}

if (dealStage) != (531, 180, 004) {


formContext.ui.sections.get("tab_12_section_9").setVisible(true);

}

else {

formContext.ui.sections.get("tab_12_section_9").setVisible(false);

}

}

  • Verified answer
    ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Hi,

    if you use this statement 

    formContext.ui.sections.get('tab_3_section_1').setVisible(false)  then you will get this error 

    Uncaught TypeError: Cannot read property 'get' of undefined

    if you use docTab variable  to setvisiblility to hide/show section and use this statement 

    docTab.ui.sections.get('tab_3_section_1').setVisible(false);  then you will get this error 
     Uncaught TypeError: Cannot read property 'sections' of undefined

    So I recommended you to use below code :

    function ShowHideDocAttachmentSections(context) {

    var formContext = context.getFormContext();

    var dealStage = formContext.getAttribute("lennar_dealstage").getValue();


    var docTab = formContext.ui.tabs.get("tab_12");


    if (dealStage != 531180000) {

    docTab.sections.get('tab_12_section_6').setVisible(true);

    }

    else {

    docTab.sections.get('tab_12_section_6').setVisible(false);

    }

    if (dealStage != 531180001) {

    docTab.sections.get('tab_12_section_7').setVisible(true);

    }

    else {

    docTab.sections.get('tab_12_section_7').setVisible(false);

    }

    if (dealStage != 531180002) {

    docTab.sections.get('tab_12_section_8').setVisible(true);

    }

    else {

    docTab.sections.get('tab_12_section_8').setVisible(false);

    }

    if (dealStage != 531180004) {


    docTab.sections.get('tab_12_section_9').setVisible(true);;

    }

    else {

    docTab.sections.get('tab_12_section_9').setVisible(true);

    }

    }

    Hope it helps

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Oooooh, I have just found the issue.

    You are calling the section via the formContext where you need to access a section via the tab you have already called.
    You retrieve the tab via "var docTab = formContext.ui.tabs.get("tab_12")".
    Now whenever you need to show or hide a section in your code you need to do "docTab.sections.get("tab_12_section_6").setVisible(true)".

    function ShowHideDocAttachmentSections(executionContext) {
        var formContext = executionContext.getFormContext();
    
        var dealStage = formContext.getAttribute("lennar_dealstage").getValue();
        var docTab = formContext.ui.tabs.get("tab_12");
        
        if (dealStage != 531180000) {
            docTab.sections.get("tab_12_section_6").setVisible(true);
        }
    }

    I would always recommend to use the doc:
    docs.microsoft.com/.../formcontext-ui-sections

    P.S.: I also noticed in the code you posted first, that you call the formContext after you are already using it. Please check the code I posted (second line), that you first need to call the formContext from the executionContext, before you use it.

    Please verify the answer if it solved the issue.
    Happy developing 

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Thank you Goutam, Evgeniy, and mardukes.  Here is a screenshot of the first section to show/hide based on Deal Stage value.  It is the correct Name.  

    pastedimage1573936685002v1.png

    Perhaps I have to set/uncheck Visibility here?

  • Martin Donnelly Profile Picture
    Martin Donnelly 1,010 on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    first number indicates line

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    The error you get "Cannot read property 'get' of undefined" must be in line 19 of your code because it say

    "at ShowHideDocAttachmentSections (lennardev.crm.dynamics.com/.../lennar_VisibilityofDocAttachSections:19:25)"

    The first digit at the end represents the line in your code and 25 five the position in this line.

    And the 'Cannot read property 'get' of undefined' means usually that you are trying to get something that is not given on the form, so as Goutam said: Check the spelling of all tab and section names you got on the form with the ones you wrote in your code.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Make sure you wrote correct section and tab name in your code.

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Thanks again. Updated the JScript in the web resource and made sure OnLoad was correct. Save and Publish as usual. I now get a 'Cannot read property 'get' of undefined. Is this due to line 3 being 'getFormContext' instead of 'getformContext'?

    TypeError: Cannot read property 'get' of undefined

       at ShowHideDocAttachmentSections (lennardev.crm.dynamics.com/.../lennar_VisibilityofDocAttachSections:19:25)

       at cp.executeFunction (lennardev.crm.dynamics.com/.../app.js

       at cp.execute (lennardev.crm.dynamics.com/.../app.js

       at rp._executeIndividualEvent (lennardev.crm.dynamics.com/.../app.js

       at rp._executeEventHandler (lennardev.crm.dynamics.com/.../app.js

       at Object.execute (lennardev.crm.dynamics.com/.../app.js

       at S._executeSyncAction (lennardev.crm.dynamics.com/.../app.js

       at S._executeSync (lennardev.crm.dynamics.com/.../app.js

       at S.executeAction (lennardev.crm.dynamics.com/.../app.js

       at t.dispatch (lennardev.crm.dynamics.com/.../app.js

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Also on your "OnLoad" configuration screenshot I see that other library is referenced for "handler" - "Sales/Opportunity...". Doublecheck that you use the correct library in registration. Don't forget to save and publish changes.

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    I updated the script in my initial reply. Can you please use updated version of code?

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: JScript to Show/Hide Sections on a form based on an Option Set value - troubleshoot

    Sure thing.  Here you go. 

    Web Resource:

    pastedimage1573856141503v1.png

    JScript:

    function ShowHideDocAttachmentSections(ExecutionContext) {

    var dealStage = formContext.getAttribute("lennar_dealstage").getValue();

    var formContext = context.getFormContext();

    var docTab = formContext.ui.tabs.get("tab_12");


    if (dealStage != 531180000) {

    formContext.ui.sections.get("tab_12_section_6").setVisible(true);

    }

    else {

    formContext.ui.sections.get("tab_12_section_6").setVisible(false);

    }

    if (dealStage != 531180001) {

    formContext.ui.sections.get("tab_12_section_7").setVisible(true);

    }

    else {

    formContext.ui.sections.get("tab_12_section_7").setVisible(false);

    }

    if (dealStage != 531180002) {

    formContext.ui.sections.get("tab_12_section_8").setVisible(true);

    }

    else {

    formContext.ui.sections.get("tab_12_section_8").setVisible(false);

    }

    if (dealStage != 531180004) {


    formContext.ui.sections.get("tab_12_section_9").setVisible(true);

    }

    else {

    formContext.ui.sections.get("tab_12_section_9").setVisible(false);

    }

    }

    Form / Handlers:

    OnLoad:

    pastedimage1573856250007v2.png

    pastedimage1573856272027v3.png

    OnChange:

    pastedimage1573856315660v4.png

    pastedimage1573856334911v5.png

    Basically the form is calling the ShowHideDocAttachmentSections from the lennar_visibilityofDocAttachSections JScript web resource...

    Hope this helps.  Thanks again.  

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans