Hide/Unhide workspace Button based on group membership

gagan12
Tera Contributor

I want to hide 'New' button on list view of Workspace.

I want to hide if user is member of 'n-XYZ' groups.(There are 20 such groups with unique name.)

There are few other groups say 'n-ABC' groups(50 such groups)

If User is also member of  'n-ABC' groups

then that button should not be hidden.

Example:- If user 'Oscar' is only part of any 'n-XYZ' (and not 'n-ABC') groups then button should be hidden.

If he is also part of any 'n-ABC' groups then button should not be hidden.

 

I have tried Script Include(using isMemberOf) and Script Condition but 2nd  part of above requirement is not working.

3 REPLIES 3

Bert_c1
Kilo Patron

post the script in the UI Action and the script from the script include, and then folks here can assist.

Udayrathore049
Tera Contributor

You can achieve this by writing a Script Condition for the button that:

  1. Checks if the user is in any n-ABC group → if yes, show the button.

  2. Else, check if the user is in any n-XYZ group → if yes, hide the button.

Priority: Being in an "ABC" group always wins, even if the user is also in "XYZ".

Here’s how you can write the Script Condition:

(function execute() {
var userID = gs.getUserID();

// Query if user is in any 'n-ABC' group
var abcGroupMember = new GlideRecord('sys_user_grmember');
abcGroupMember.addQuery('user', userID);
abcGroupMember.addQuery('group.name', 'STARTSWITH', 'n-ABC');
abcGroupMember.setLimit(1); // We only care if there is at least one
abcGroupMember.query();
if (abcGroupMember.hasNext()) {
// User is in an 'n-ABC' group, so do NOT hide button
return false;
}

// Query if user is in any 'n-XYZ' group
var xyzGroupMember = new GlideRecord('sys_user_grmember');
xyzGroupMember.addQuery('user', userID);
xyzGroupMember.addQuery('group.name', 'STARTSWITH', 'n-XYZ');
xyzGroupMember.setLimit(1);
xyzGroupMember.query();
if (xyzGroupMember.hasNext()) {
// User is in 'n-XYZ' group but not in 'n-ABC', so hide button
return true;
}

// Otherwise, user is not in any relevant groups, do not hide
return false;
})();

How it Works:

  • It first checks if the user belongs to any "n-ABC" group.

    • If yes → return false (do not hide).

  • If not, then it checks if the user belongs to any "n-XYZ" group.

    • If yes → return true (hide).

  • If neither, return false (show the button).

 

Additional Tips:

  • Use setLimit(1) to optimize queries — you don't need to check all memberships, just whether at least one exists.

  • Make sure to cache or reuse this logic if used in multiple places, e.g., via a Script Include.

  • Always test with users in:

    • Only XYZ group → Button should hide.

    • Only ABC group → Button should show.

    • Both groups → Button should show.

    • Neither group → Button should show.

Ankur Bawiskar
Tera Patron
Tera Patron

@gagan12 

Why not control this via the Table level CREATE ACL?

You can use script in the advanced section

Share screenshots of what you tried so far and what debugging did you perform?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader