onShow Script not working as expected - Maybe I doing something wrong

Imran1
Giga Guru

Hi All,

 

I am trying to restrict Export options for two specific roles. I am using the onShow script within ui_context_menus

the Export in the list is disabled however the action type of exports like Excel, PDF, etc. is not getting disabled.

When I simply use condition instead of the script as an admin I am not able to view the Export option. The requirement is to disable export to those two roles.

Not sure what I am doing wrong. Appreciate your assistance in fixing this.

onShow();
function onShow() {
    // Check if the user has either of the restricted roles
    var userHasRestrictedRole = g_user.hasRoleExactly('Role_1') && g_user.hasRoleExactly('Role_2');
    
    // Use alert for debugging
    alert('User has restricted role: ' + userHasRestrictedRole);
    
    // Enable or disable the menu item based on the role check
    if (userHasRestrictedRole) {
        g_menu.setDisabled(g_item);
        
        // Disable specific child menu items by their IDs or names
        var childItems = ['export_excel', 'export_csv', 'export_json', 'export_xml', 'export_pdf'];
        for (var i = 0; i < childItems.length; i++) {
            var childItem = g_menu.getItem(childItems[i]);
            if (childItem) {
                g_menu.setDisabled(childItem);
            }
        }
    } else {
        g_menu.setEnabled(g_item);
        
        // Enable specific child menu items by their IDs or names
        var childItems = ['export_excel', 'export_csv', 'export_json', 'export_xml', 'export_pdf'];
        for (var i = 0; i < childItems.length; i++) {
            var childItem = g_menu.getItem(childItems[i]);
            if (childItem) {
                g_menu.setEnabled(childItem);
            }
        }
    }
}

Regards,

Imran

1 ACCEPTED SOLUTION

My bad, hasRoleExactly isn't available to use server-side. 

 

Try with this instead, it checks that the user does not have either Role 1 or Role 2 (make sure to use lowercase values in your condition). I tested in my PDI and Admin can still see the Export menu, but if they are given one of those roles then it is removed. 

!ListProperties.isRelatedList() && !ListProperties.isRefList() && !(gs.getSession().getRoles().toString().includes("role_1") || gs.getSession().getRoles().toString().includes("role_2"))

 

View solution in original post

4 REPLIES 4

Matthew Smith
Kilo Sage

If I've understood the requirement correctly, you just want to Hide the Export (and child menu items) from any user that has either Role_1 or Role_2?

 

If so, then you can just do this in the Condition field like so:

 

MatthewSmith_0-1724236229606.png

- Matt

 

Hi Matthew,

 

I had this same in the condition field !gs.hasRole("Role_1 , Role_2") 

!gs.hasRole("Role_1") &&  !gs.hasRole("Role_2") but it works for the user and others but those who have admin roles for them it doesn't show export button. 

I verified the users' role including myself I don't have either of Role_1 or Role_2 added to my profile.

I am wondering why admin user doesn't see Export context menus

 

Regards,

Imran

My bad, hasRoleExactly isn't available to use server-side. 

 

Try with this instead, it checks that the user does not have either Role 1 or Role 2 (make sure to use lowercase values in your condition). I tested in my PDI and Admin can still see the Export menu, but if they are given one of those roles then it is removed. 

!ListProperties.isRelatedList() && !ListProperties.isRefList() && !(gs.getSession().getRoles().toString().includes("role_1") || gs.getSession().getRoles().toString().includes("role_2"))

 

Thank You Matthew, It worked. 

one clarification: Why should we use these? ListProperties.isRelatedList() && !ListProperties.isRefList() and what purpose do they serve?