Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help with population of roles and groups in catalog item

shaik riyaz1
Tera Contributor

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

 

1 ACCEPTED SOLUTION

@shaik riyaz1 

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! 🙏

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

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@shaik riyaz1 

Ideally you should do this

1) 1st variable point to sys_user_role table with advanced ref qualifier as this

javascript: var query;
var cu = gs.getUser();
query = 'nameIN' + new global.ArrayUtil().convertArray(cu.getRoles());
query;

2) 2nd variable point to sys_user_group, list collector type

refer my blog on how to set list collector based onchange + GlideAjax

Dynamically set list collector on change of variable 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@shaik riyaz1 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar ,
Thanks for your response.

The code is working fine for current logged-in user, but my requirement is to fetch the roles based on the user selected in the 'Requested for' variable instead of logged-in user.

Could you please suggest?

@shaik riyaz1 

Glad to know that my script worked for you.

you required logged in user as per your original requirement

AnkurBawiskar_0-1762847283211.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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