Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Can't get simple tab show/hide code to work

(0) ShareShare
ReportReport
Posted on by 70
I have a tab (SalesUpdate) that i want to hide/show when there is a lookup field (new_change) value present but hide the tab (SalesUpdate) when it's blank. I kept running into errors with that so decided to set an option set field (new_type) via business rule based on if that lookup field (new_change) contains data. I ended up with the following code and this still doesn't work. I'm getting a is not defined at eval error.
function showHideTabs(){
	var type = Xrm.Page.getAttribute("new_type").getValue();
	
	if (type == 172960001){
		Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(true);
	}
	else {
		Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
	}
}
Is there someone that can help with a simple solution that won't error? If looking at the lookup field to be null or contains data is easiest please use that but if the options set is easiest please use that. I'm not hooked into one solution. Thanks in advance. 

*This post is locked for comments

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Can't get simple tab show/hide code to work

    Following is enough:

    var type = Xrm.Page.getAttribute("new_type").getValue();

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: Can't get simple tab show/hide code to work

    Thanks Andrew for your suggestion  , I understand what you are saying I have write two method -Let me know is this correct or not .

    //Using getSelectedOption().value

    function showHideTabs(){ 
        var type = null;
        try {
            type = Xrm.Page.getAttribute("new_type").getSelectedOption().value;
        }
        catch (ex) {
            type = null;
        }
       
        if (type != null & type != undefined) {
            if (type == "172960001"){
                Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(true);
            }
            else {
                Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
            }
        }
        else
        {
            Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
        }
    }

    // Using getValue()

    function showHideTabs(){  
        try {
            var typeObj = Xrm.Page.ui.controls.get("new_type");
            var type = typeObj.getAttribute().getValue();
            if (type != null & type != undefined) {
                if (type == "172960001"){
                    Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(true);
                }
                else {
                    Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
                }
            }
            else
            {
                Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
            }
        }
        catch (ex) {
            Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
        } 
    }

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Can't get simple tab show/hide code to work

    Check you provided has no sense for scenario when value of optionset was changed to null. Line before your check will generate an exception and your checks will not work.

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: Can't get simple tab show/hide code to work

    Yes Andrew you are right ,  but the function will fire onchange expected that value should not be null.

    Anyway its always good for safe side to check the null or undefined .

    function showHideTabs(){ 
        var type = Xrm.Page.getAttribute("new_type").getSelectedOption().value;
        if (type != null & type != undefined) {
            if (type == "172960001"){
                Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(true);
            }
            else {
                Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
            }
        }
    }


  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Can't get simple tab show/hide code to work

    Goutam,

    I believe this line

    var type = Xrm.Page.getAttribute("new_type").getSelectedOption().value;

    will generate exception if no value is selected. Am I right or not?

  • Verified answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: Can't get simple tab show/hide code to work

    Hi Chris ,

    Try with this -

    function showHideTabs(){ 
        var type = Xrm.Page.getAttribute("new_type").getSelectedOption().value;
        if (type == "172960001"){
            Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(true);
        }
        else {
            Xrm.Page.ui.tabs.get("SalesUpdate").setVisible(false);
        }
    }


  • Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: Can't get simple tab show/hide code to work

    There is nothing wrong in your code.It may seem trivial but you need to use tab name

    while referring the control not the label

    Xrm.Page.ui.tabs.get({{tab_name}}).setVisible(true);

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans