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

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)
  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at
  • Chrisbra22 Profile Picture
    on at

    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
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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

    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!  

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Please check below link for the sample code

    crmlogs.wordpress.com/.../

  • Chrisbra22 Profile Picture
    on at

    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. 

  • Chrisbra22 Profile Picture
    on at

    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);

    }

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Good to hear that it worked 

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 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans