We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

get list of groups user belongs to

gtk
Mega Sage

we have created a new custom table with name u_work_group

in which it is having list of 50+ groups, and now we got a requirement that

create a field which refers to sys_user table

and when user is selected then ln a text box to be shown with list of groups that user is member of

agent - type is reference - points to sys_user

groups - type is text box - get group of user

(groups to be separated with comma [,])

16 REPLIES 16

sachin_namjoshi
Kilo Patron

Your can create on change client script to populate group text field field group id seprated by comma.



Regards,


Sachin


On which table I need to write client script

Somveer Karhan1
Giga Expert

Use below onChange client script on agent field.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }


var groups = [];


    var gr = new GlideRecord('sys_user_grmember');


      gr.addQuery('user',newValue);


      gr.query();


      while(gr.next())


              {


                      groups.push(gr.group);


              }


    g_form.setValue('u_groups',groups);


}


On which table we have to write this script

Dave Smith1
ServiceNow Employee

gtk wrote:



we have created a new custom table with name u_work_group


in which it is having list of 50+ groups, and now we got a requirement that


create a field which refers to sys_user table


and when user is selected then ln a text box to be shown with list of groups that user is member of


... erm.. this feels like duplicating existing functionality.   Why create a separate table with similar information then try to relate/sync them?



It sounds like what you're after is a reference qualifier to limit down enumerated picklists.