web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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);

}

}

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try following updated 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) {
    
    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);
    
    }
    
    }

  • Martin Donnelly Profile Picture
    1,030 on at

    So, you can get away with declaring dealStage before formContext?!

  • William Bradley Profile Picture
    175 on at

    Thanks Andrew.  I implemented this onLoad of form and OnChange of Deal Stage, passing execution for each, and it did not work.  Still same error.  Should dealStage be moved up?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Can you please provide a screenshot where you register handler?

  • William Bradley Profile Picture
    175 on at

    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.  

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • William Bradley Profile Picture
    175 on at

    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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

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

  • Suggested answer
    Community Member Profile Picture
    on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans