
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2020 04:49 PM
I'm building some catalog items for ServiceNow group maintenance. The requirement is that users can only make requests for groups of which they are already a member. If a user is a member of only one group, set the group variable to that group. I'm running a catalog client script with a GlideRecord query on sys_user_grmember, looking for records where the group is a specific type, and contain the logged in user. For some reason, when the user is a member of only one group, the query acts as though they are members of no groups. If they are a member of multiple groups, it returns all of their groups, so it's not like it's just skipping the first one somehow. I'm stumped.
var count = 0;
var userID = g_user.userID; //get the currently logged in user
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group.type','CONTAINS','4785e3446fa2310026da31012e3ee4c1'); // the sys_id of the group we care about
grp.addQuery('user', userID);
grp.query(callback);
function callback(grp){
while (grp.next()) {
count++;
}
if (count == 1) {
g_form.setValue("doc_sn_group", grp.group);
g_form.setReadOnly('doc_sn_group', true);
} else if(count==0){
g_form.addInfoMessage('No group found');
} else {
g_form.addInfoMessage('More than one group found');
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2020 03:29 PM
Seems like you're getting it from here. I feel you'll have better results now.
Let me know if you need anything else?
If my answer helped guide you correctly, please mark it as Helpful & Correct.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2020 10:36 PM
Hi Ron
You structure is wrong - see my comments in the code
function callback(grp){
//Here you loop through all group member records
while (grp.next()) {
count++;
}
//done looping through all records - now theres no group records left which causes your issue
if (count == 1) {
g_form.setValue("doc_sn_group", grp.group);
g_form.setReadOnly('doc_sn_group', true);
} else if(count==0){
g_form.addInfoMessage('No group found');
} else {
g_form.addInfoMessage('More than one group found');
}
}
As stated above you should do this in a GlideAjax.
Furthermore then you should make GlideAggregate instead of GlideRecord as you really only uses it to count the amount of records and sys_id's are available here as well (uses alot less resources server side)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2020 09:12 AM
Sorry - what I didn't make clear in my original post is that if there's only one result, the script doesn't even enter the 'while' loop - it acts as though there are no records returned.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2020 11:15 PM
Hi Ron,
So you have a group variable which is a reference to sys_user group table and you want it to work like below
1) if logged in user is member of only 1 group of type -> 4785e3446fa2310026da31012e3ee4c1 auto populate that value
2) if logged in user is member of more than 1 group of type -> 4785e3446fa2310026da31012e3ee4c1 then show only those groups;
For the 1st part you would require onLoad Catalog Client Script with combination of Script Include + GlideAjax to set the value
For the 2nd part you would require advanced reference qualifier on the group variable so that when logged in user selects the group only those groups of type "4785e3446fa2310026da31012e3ee4c1" where he/she is member of is shown
GlideRecord in client script is not recommended approach.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader