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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Hide Related tab in Contact based on security role

(4) ShareShare
ReportReport
Posted on by 5,514
Hi All,
 
I have a client requirement where
I need to hide a RELATED tab in contact form if the user is not assigned with "d365_business user " and "d365_businesssupport" security role.
si this achievable if yes how?
 
 
 
 
 
Thanks
Sandeep
Categories:
I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at
    Hi Sandeep,

    Can you tell us what Dynamics 365 application this question is related to? There are several different solutions in the Dynamics 365 family, like: Sales, Finance, Supply Chain Management, Project Operations, and Business Central. If you can clarify the product, we can move the question to the dedicated forum for that solution and help you with the correct answer.
  • Verified answer
    Dharanidharan Profile Picture
    638 Super User 2025 Season 2 on at
    Hi Sandeep,
     
    Yes, this is achievable using JavaScript in the contact form. You can check the user's security roles on form load and hide the RELATED tab if they do not have the required roles.
    Here's a sample JavaScript function to achieve this:
    function hideRelatedTabBasedOnSecurityRole(executionContext) {
        var formContext = executionContext.getFormContext();
        var userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
        var requiredRoles = ["d365_business user", "d365_businesssupport"];
        
        var hasRequiredRole = userRoles.some(function (role) {
            return requiredRoles.includes(role.name);
        });
    
        if (!hasRequiredRole) {
            formContext.ui.tabs.get("tab_name").setVisible(false);
        }
    }
    
    Steps to Implement:
    1. Replace "tab_name" with the actual logical name of the RELATED tab.
    2. Add this function to the OnLoad event of the contact form in PowerApps.
    3. Ensure the web resource containing this script is published and associated with the form.
    Let me know if you need further clarification.
  • Ramesh Kumar Profile Picture
    7,527 Super User 2025 Season 2 on at
    Can you please help provide which application or ERP we are talking about
     
    Thanks
    Ramesh
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    7,835 Super User 2025 Season 2 on at
    Hello Sandeep,
     
    Yes, this requirement is absolutely achievable in Dynamics 365 using JavaScript and web resources.
     
    Here's a step-by-step guide on how to hide a related tab on the Contact form based on user security roles:
    1. Identify the Tab and Security Roles:
    • Tab Name: Determine the exact name of the related tab you want to hide. You can find this by opening the Contact form editor and looking at the tab's properties.
    • Security Roles: Confirm the exact names of the security roles ("d365_business user" and "d365_businesssupport") that should have access to the tab.
    2. Create a JavaScript Web Resource:
    • Create a New Web Resource:
      • In Dynamics 365, go to Settings > Customizations > Customize the System.  
      • Expand Web Resources and click New.
      • Display Name: Give the web resource a descriptive name (e.g., "HideRelatedTabBasedOnRole").
      • Name: Give the web resource a schema name that is unique.
      • Type: Select "Script (JScript)".
      • Text Editor: Click the "Text Editor" button.
    • Add JavaScript Code:
      • Paste the following JavaScript code into the text editor:
     
    JavaScript (Example)
     
        function hideRelatedTabBasedOnRole(executionContext) {
            var formContext = executionContext.getFormContext();
            var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
            var tabName = "YOUR_RELATED_TAB_NAME"; // Replace with your tab name
            var allowedRoles = ["d365_business user", "d365_businesssupport"];

    var userHasAllowedRole = false;
            for (var i = 0; i < userRoles.length; i++) {
                var roleName = Xrm.Utility.getGlobalContext().securityRoles.getById(userRoles[i]).name;
                if (allowedRoles.indexOf(roleName) !== -1) {
                    userHasAllowedRole = true;
                    break;
                }
            }

    if (!userHasAllowedRole) {
                formContext.ui.tabs.get(tabName).setVisible(false);
            }
        }

     
    • Replace Placeholder:
      • Replace "YOUR_RELATED_TAB_NAME" with the actual name of your related tab.
    • Save and Publish:
      • Click OK to close the text editor.
      • Save and then Publish the web resource.
    3. Add the Web Resource to the Contact Form:
    • Open the Contact Form Editor:
      • In Dynamics 365, go to Settings > Customizations > Customize the System.  
      • Expand Entities > Contact > Forms.
      • Open the Contact form you want to modify.
    • Add the Web Resource:
      • In the form editor, click Form Properties.
      • Go to the Events tab.
      • In the Form Libraries section, click Add.
      • Select the web resource you created ("HideRelatedTabBasedOnRole").
      • Click Add.
    • Add the OnLoad Event Handler:
      • In the Event Handlers section, click Add.
      • Event: Select "OnLoad".
      • Library: select the web resource you added.
      • Function: Enter hideRelatedTabBasedOnRole.
      • Check the "Pass execution context as first parameter" checkbox.
      • Click OK.
    • Save and Publish:
      • Click OK to close the Form Properties dialog.
      • Save and then Publish the Contact form.
     
    Explanation:
    • Xrm.Utility.getGlobalContext().userSettings.securityRoles: This retrieves an array of the user's security role GUIDs.  
    • Xrm.Utility.getGlobalContext().securityRoles.getById(userRoles[i]).name: This retrieves the name of a security role based on its GUID.
    • formContext.ui.tabs.get(tabName).setVisible(false): This hides the specified tab.
    • The code loops through the users security roles, and checks if any of the users roles match the allowed roles. If the user does not have an allowed role, then the tab is hidden.
     
    Important Notes:
    • Tab Name: Make sure you enter the correct tab name in the JavaScript code.
    • Security Role Names: Ensure the security role names are spelled correctly and match the exact names in Dynamics 365.
    • Testing: Thoroughly test the functionality with different users and security roles.
    • Publishing: Always remember to publish your changes after making modifications.
    • Performance: For complex forms with many security role checks, consider optimizing your JavaScript code for performance.
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Best Regards,
    Daivat Vartak
  • sandeepc Profile Picture
    5,514 on at
     
    I think related tab is not general tab to have tab name on form. i cannot see related tab in backend. i need to hide this related tab
  • sandeepc Profile Picture
    5,514 on at
    CRM sales
  • Suggested answer
    Tom_Gioielli Profile Picture
    2,762 Super User 2025 Season 2 on at
    You can hide entries on the related tab, but it cannot be done dynamically by checking security role (the exception to this is if the users do not have access to the related table without the above roles, then the entry will not appear).
     
    I might recommend the following to meet the requirement.
    • Remove the table from the Related tab (I'm 90% sure you can do this in the new maker portal, but you can definitely do it in the classic version)
    • Add a new tab onto the form with the name of this related table and add a subgrid to show the records. Basically, take it out of the related section and add it straight to your form
    • User JavaScript to check user security role associations on load of the form, and hide/show the tab with your records dynamically based on those results
     
    I will add that it may be possible to access the "Related" tab and the menu options through JavaScript directly (and thus bypass steps 1 and 2 above), but I am not sure if it can be done or how it would be done. The code listed in a response below will not work, because the related section is not a traditional tab and thus cannot be access through that method. as far as I know.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans