Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

How to hide tabs based on field value

Posted on by 35

I have this code created but want to add to it for if the same field is another value, show another tab. How do I add conditions to what I already have? 

function Industry() {
//show Construction tab if the Account Type = Construction and the Sub Type = Public
var AccountTypeValue = Xrm.Page.getAttribute("accountypecode").getValue();
var SubTypeValue = Xrm.Page.getAttribute("grey_subtype").getValue();

var industrytab = Xrm.Page.ui.tabs.get("IndustryTab");

if (SubTypeValue != null && SubTypeValue == 3) {

//show the section
industrytab.setVisible(true);
}
else {
//hide the section
industrytab.setVisible(false);
}
}

this code works but I need another condition where if account type = public sector show a public sector tab. I know it’s the same script but how do I include this script within the existing script. Anything I try gives me an error as it is outside the function

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to hide tabs based on field value

    Hi,

    Please go through below link you will get some idea.

    juniorcrmblog.blogspot.com/.../how-to-hide-and-show-tab-based-on.html

    juniorcrmblog.blogspot.com/

    www.youtube.com/.../videos

    Thanks,

    Arshad

  • Suggested answer
    Vipin J Profile Picture
    Vipin J 1,582 on at
    RE: How to hide tabs based on field value

    Generic Method to Hide and Show Tab and Section  - https://vjcity.blogspot.com/2021/07/generic-method-to-hide-and-show-tab-and.html

    How to hide a section in MS CRM 2016 based on user logged in ?  - https://vjcity.blogspot.com/2016/09/how-to-hide-section-in-ms-crm-2016.html

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 Moderator on at
    RE: How to hide tabs based on field value

    Hi,

    I am not sure how it is working. You are missing Account Type = Construction check condition in your code.

    As per your comment - //show Construction tab if the Account Type = Construction and the Sub Type = Public

    but in your check condition you are only checking subtype

    if (SubTypeValue != null && SubTypeValue == 3) {

    I hope this is not intentional and happened by mistake.

    Now coming to your original question where you would like to add another codnition to show Public tab - I have modified your code as below, please try this -

    function Industry(executionContext) {

    var formContext=executionContext.getFormContext();

    //show Construction tab if the Account Type = Construction and the Sub Type = Public

    var AccountTypeValue = formContext.getAttribute("accountypecode").getValue();

    var SubTypeValue = formContext.getAttribute("grey_subtype").getValue();

    var industrytab = formContext.ui.tabs.get("IndustryTab");

    var publictab = formContext.ui.tabs.get("PublicTab");

    if (SubTypeValue != null && SubTypeValue == 3) {

    //show the section

    industrytab.setVisible(true);

    // Hide Public Tab

    publictab.setVisible(false);

    }

    else if(AccountTypeValue!=null && AccountTypeValue==Specify Public Optionset valie)

    {

    industrytab.setVisible(false);

    publictab.setVisible(true);

    }

    else {

    //hide the section

    industrytab.setVisible(false);

    publictab.setVisible(false);

    }

    }

    You are also missing 't' in accountypecode field schema name. Please verify this anfd fix your schema name.

    Make sure to select pass execution context as first parameter when you register your event handler.

    CR46.PNG

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: How to hide tabs based on field value

    Hi,

    Will it be any issue if you are writing else if condition? There is no option to optimize the code. And I would recommend don't use Xrm.Page as its deprecated

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…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 228,683 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans