GlideRecord query on sys_user_grmember fails if only one record returned

Ron Legters
ServiceNow Employee
ServiceNow Employee

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');
	}
    }
1 ACCEPTED SOLUTION

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!

View solution in original post

12 REPLIES 12

Tony Chatfield1
Kilo Patron

Hi, have you added any debugging to the while clause to confirm that grp.group returns a result?
or tried grp.group.sys_id in your setValue - possibly grp.group.toString()

I did have some g_form.addInfoMessages in this just to watch how it was processing, and when the query returned only one record, it didn't go to the while clause. It only went to the while clause when there was more than one.

Allen Andreas
Administrator
Administrator

Hi,

A few things (none of which are meant rudely, just me talking/asking questions):

1) Please don't use GlideRecord in a client script. That's not best practice and shouldn't be done. You'd instead need to use GlideAjax and conduct your server query that way...I'm sure the callback function is your angle you'll go with for saying it's fine, but really, it just shouldn't be done or a habit, just throwing that out there.

2) Can you define: "the query acts as though they are members of no groups"?

For #2 do you mean it's resulting in your else if statement? Or it's not just populating the group?

3) grp.addQuery('group.type','CONTAINS','4785e3446fa2310026da31012e3ee4c1'); // the sys_id of the group we care about -- is this the sys_id of a group...or a sys_id of a group type? Your comment says otherwise...

4) When you said: "For some reason, when the user is a member of only one group" -- just to clarify again...that you mean they are a member of only one group which is the TYPE you're looking for, right? Because I already have a concern mentioned above in #3 and then how you speak here also makes me wonder if there's something else going on.

5) You said: "If they are a member of multiple groups, it returns all of their groups" -- Not sure how you're coming to that conclusion per the infomsgs above you're not capturing specifics at all. So I don't know if you can safely say "it returns all of their groups". Unless you get a print out of the groups as they're found, you won't know.

From my experience...you're also accessing the count variable from outside the function and that may cause some issues. Try declaring it inside the function even 1 line in. So somehow it's working for more than 1 because it has time to trickle up the count, but if it doesn't start with a 0 initially due to living outside the function, it's probably not 1 when it goes through the while loop that first time.

To confirm...you can add an alert to your count and see?

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thank you for taking the time to reply. To your questions

1. Noted. I wasn't having success with GlideAjax, either, but from your and other replies, it sounds like that's where I should focus my efforts.

2. I had an info message in the 'while' loop, and when there was only result, it didn't look like we were entering the while loop at all.

3. Bad comment - it is, in fact, the sys_id of the group type, not the group.

4. Yes - when the user is a member of only group of the specified type.

5. Another info message that I deleted for purposes of keeping the code here cleaner - in the while loop, I was actually displaying the groups as they looped.

Last paragraph - I see your point here, but it's not entering the while loop at all when there's only one result. (which I didn't make clear in my first post)