Skip to main content

Notifications

Dynamics 365 general forum
Suggested answer

How to hide tabs using java script

Posted on by 5

Can anyone help please ?

I have search other similar queries but still cannot get a working answer.

On a Form there is a field (Form Type) which has 2 choices.  I want to show/hide tabs (Tab_3, Tab_4 and Tab_5) depending on the option selected - hide all three or show all three.  The original script was hoping to OnLoad (of the form) hide all three tab, and then OnChange of the field show or hide depending on the option.

Whatever I have tried I get the error "Cannot read properties of undefined (reading 'getFormContext') - I have tried changing so may parts of the script - cutting it right down to just OnChange and just one Tab - still not working.

This is the code as it is now

function ToggleTab(executionContext) {
var formContext = executionContext.getFormContext();

var tabvalue = formContext.getAttribute("crm365_formtype").getValue();
formContext.ui.tabs.get("tab_5").setVisible(false);
formContext.ui.tabs.get("tab_3").setVisible(false);
formContext.ui.tabs.get("tab_4").setVisible(false);

if (tabvalue == 154220000)
formContext.ui.tabs.get("tab_5").setVisible(true);
formContext.ui.tabs.get("tab_3").setVisible(true);
formContext.ui.tabs.get("tab_4").setVisible(true);

if (tabvalue == 154220001)
formContext.ui.tabs.get("tab_5").setVisible(false);
formContext.ui.tabs.get("tab_3").setVisible(false);
formContext.ui.tabs.get("tab_4").setVisible(false);
}

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: How to hide tabs using java script

    Your initial code missing some curly brackets. Can you pleas provide your current version of the code and screen that demonstrates how you registered the event handler?

  • ONE BEE Profile Picture
    ONE BEE 15 on at
    RE: How to hide tabs using java script

    for me same error continues

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to hide tabs using java script

    Hi PMM001,

    I created a Yes/No type field(Form Type) to test your code. It works fine. Below is my code:

    onload event:

    Snipaste_5F00_2022_2D00_02_2D00_08_5F00_09_2D00_25_2D00_07.png

    function hideAllTabs(executionContext) { 
    	var formContext = executionContext.getFormContext();
            formContext.ui.tabs.get("tab_5").setVisible(false);
    	formContext.ui.tabs.get("tab_4").setVisible(false);
    	formContext.ui.tabs.get("tab_3").setVisible(false);
    }
    
    

    onchange event on Form Type field:

    Snipaste_5F00_2022_2D00_02_2D00_08_5F00_09_2D00_27_2D00_27.png

    function ToggleTab(executionContext) { 
    	var formContext = executionContext.getFormContext();
    
    	var tabvalue = formContext.getAttribute("cr2f9_formtype").getValue();
    	formContext.ui.tabs.get("tab_5").setVisible(false);
    	formContext.ui.tabs.get("tab_3").setVisible(false);
    	formContext.ui.tabs.get("tab_4").setVisible(false);
    
    	if (tabvalue){
    		formContext.ui.tabs.get("tab_5").setVisible(true);
    		formContext.ui.tabs.get("tab_3").setVisible(true);
    		formContext.ui.tabs.get("tab_4").setVisible(true);
    	}else if (!tabvalue){
    		formContext.ui.tabs.get("tab_5").setVisible(false);
    		formContext.ui.tabs.get("tab_3").setVisible(false);
    		formContext.ui.tabs.get("tab_4").setVisible(false);
    	}
    }
    
    

    Result:

    tab.gif

    You could check the setting as the screenshot shows. If they are correct, please check whether there are any other event handler on your table and is it correct?

  • PMM001 Profile Picture
    PMM001 5 on at
    RE: How to hide tabs using java script

    No that was ticked - believe it or not I suddenly remembered something from years ago - changed the function name to begin with a lowercase and the error went away.  Need more tweaking to get it fully working but sorted now :)

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: How to hide tabs using java script

    from the error my best guess is that the option "pass execution context as first parameter" has not been checked when you attached the javascript functions to the onload and onchange events

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,469 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans