- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
I’m working on a catalog item where I need to configure two fields with the below behavior:
Field Name: Roles assigned to the user
Requirement: It should show the list of all roles assigned to the logged-in user(Requested for) field, basically fetched from the sys_user_has_role table.
Field Name: Groups associated with the selected role
Type: Multi-select dropdown
Requirement:
When a role is selected in the “Roles assigned to user” field,
the system should display all groups associated with that role (in which the user is a member).
All those groups should be auto-selected by default, but the user should be able to deselect if needed.
I’m trying to implement this logic using a catalog client script and script include, but I’m not sure about the best approach to dynamically populate the multi-select dropdown based on the selected role.
Has anyone implemented something similar or can guide on the right way to achieve this (preferably without using gel or DOM methods)?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Ensure the default value for requested_for has gs.getUserID() so that logic works when form loads for logged in user and then it will work based on user change
if you want based on requested for variable and user will select that then do this
javascript: var query;
var cu = gs.getUser().getUserByID(current.variables.requested_for);
query = 'nameIN' + new global.ArrayUtil().convertArray(cu.getRoles());
query;
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Ensure the default value for requested_for has gs.getUserID() so that logic works when form loads for logged in user and then it will work based on user change
if you want based on requested for variable and user will select that then do this
javascript: var query;
var cu = gs.getUser().getUserByID(current.variables.requested_for);
query = 'nameIN' + new global.ArrayUtil().convertArray(cu.getRoles());
query;
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankur Bawiskar ,
The code is working fine for the 'roles assigned to the user' , Thank you
Regarding #2 i have gone through the blog and my requirement is 'For the selected role in the 'Roles assigned to user' field, the system will display all groups associated with that role in which the user is a member.
All the groups related to that role will be auto-selected, which can be deselected by User if needed'
I have tried with below code but its not working. It always shows all groups. Also, I think I am unable to clear all options , tried clearValue(), clearOptions().
Script include:
Catalog client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
2nd variable I recommended to have list collector type referring to sys_user_group
Only list collector allows you multi-selection
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
