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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Javascript example needed - Show Tab when Related Entity contains data

(0) ShareShare
ReportReport
Posted on by

Hi, 
I have a requirement to show the 'Create PO' tab only when the related entity "Work Order" contains a record - else hide. 
Has anyone encountered this example before and have a sample code for me to get started?  Many thanks in advance.  

pastedimage1669132028418v2.png

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Hi,

    Good to hear that it worked 

  • Chrisbra22 Profile Picture
    on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Update.  Got this to work:

    function ShowHideMasterPOMatchPOTabs(executionContext) {

     var formContext = executionContext.getFormContext();

     var isRole = false;

     Xrm.Utility.getGlobalContext().userSettings.roles.forEach(

       role => {

         if(

           role?.name == "System Administrator" ||

           role?.name == "*BF Field Service - Zone Manager" ||

           role?.name == "*BF Field Service - Dispatcher"

         ) {

           isRole = true;

         }

       }

     );

     formContext.ui.tabs.get("tab_4").setVisible(isRole);

    }

  • Chrisbra22 Profile Picture
    on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Thanks, I did see that article but the code was not working for me.  In the article above the code is hiding all related navigation items if User Security Role is not System Admin.  

    Basically I need the code to show the 'Match PO' Tab (tab_4) only to the roles System Admin (System Administrator), Zone Manager (*BF Field Service - Zone Manager), Dispatcher (*BF Field Service - Dispatcher), else it hides. 
    Any suggestions here?  I've tried and am not successful thus far. 

  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Hi,

    Please check below link for the sample code

    crmlogs.wordpress.com/.../

  • Chrisbra22 Profile Picture
    on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Thanks!  Will check that out.  

    Another question is if there is a code snippet to show a tab (tab_4) if User Security Role = ("System Administrator"), ("*BF Field Service - Dispatcher"), (*BF Field Service - Zone Manager"), else hide.

    I have a snippet that is not working I think because the !UserHasRole is not properly being used, or out of date.  

    Any thoughts or sample would be appreciated!  

  • Verified answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Hello,

    Use Xrm.WebApi.retrieveMultipleRecords to check the record count in related entity

    learn.microsoft.com/.../retrievemultiplerecords

    Dataverse Rest builder tool can be used to generate code

    carldesouza.com/.../

  • Chrisbra22 Profile Picture
    on at
    RE: Javascript example needed - Show Tab when Related Entity contains data

    Thanks!  I did see some of that documentation.  I have just never wrote code to show/hide tabs based on a related entity having data (as shown in my screenshot above).  I'll keep digging, thanks.  

  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 141

#2
DAnny3211 Profile Picture

DAnny3211 134

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans