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

Lawrence29
Tera Contributor

Hi How did you solve you use case?

SAI VENKATESH
Kilo Patron

Hi @gtk 

 

You can try the below code it will print all the groups that user belongs to

(function executeRule(current, previous /* previous */) {
    var userId ='a8f98bb0eb32010045e1a5115206fe3a'; //change to field name 


    var userGroups = new GlideRecord('sys_user_grmember');
    userGroups.addQuery('user', userId);
    userGroups.query();
    var groupNames = [];
    while (userGroups.next()) {
        var groupName = userGroups.group.getDisplayValue();
        groupNames.push(groupName);
    }
    current.groups = groupNames.join(', ');

})(current, previous);

 

Thanks and Regards

Sai Venkatesh