To populate list of groups based on user on employee profile form

lakshmi_laksh
Tera Contributor

Hello,
I have a requirement , when a user is selected from user field, the user added to particular groups list need to populated in group list field, so that more than one group can be selected.
if anyone can help me on this would be helpfull.
thank you

lakshmi_laksh_0-1745838301767.png

 

1 ACCEPTED SOLUTION

@lakshmi_laksh 

you can use script in the advanced ref qualifier as this so that it shows only the groups for that user which are having pps_resource role

Something like this

javascript: var query = '';
var userGroups = [];
var groups = new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.userField).getMyGroups());
// Query to check if the group has the pps_resource role
var grGroupRole = new GlideRecord('sys_group_has_role');
grGroupRole.addQuery('group', 'IN', groups.toString());
grGroupRole.addQuery('role.name', 'pps_resource');
grGroupRole.query();
while (grGroupRole.next()) {
    userGroups.push(grGroupMember.group.toString());
}
query = 'sys_idIN' + userGroups.toString();
query;

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@lakshmi_laksh 

you can use advanced reference qualifier on that list field.

I assume list field refers to sys_user_group table

Something like this, ensure you give correct user field here

   javascript: 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.userField).getMyGroups()); 

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

Hi Ankur Bawiskar,
thank you for the response,
but i need script because i need to filter to display only groups with "pps_resource" role and if current user is removed from any group or added should reflect on groups list as well 

@lakshmi_laksh 

you can use script in the advanced ref qualifier as this so that it shows only the groups for that user which are having pps_resource role

Something like this

javascript: var query = '';
var userGroups = [];
var groups = new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.userField).getMyGroups());
// Query to check if the group has the pps_resource role
var grGroupRole = new GlideRecord('sys_group_has_role');
grGroupRole.addQuery('group', 'IN', groups.toString());
grGroupRole.addQuery('role.name', 'pps_resource');
grGroupRole.query();
while (grGroupRole.next()) {
    userGroups.push(grGroupMember.group.toString());
}
query = 'sys_idIN' + userGroups.toString();
query;

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

@lakshmi_laksh 

Hope you are doing good.

Did my reply answer your question?

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