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

Hi,

Understood.

Please mark any reply as Helpful, thus far, if it was.

I would personally recommend going the route of a script include, here's a cheat sheet to try and attempt this again?

https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

I think the whole function thing may be throwing some of this off.

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


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

I appreciate your help. Another question:

Why isn't this client script calling the script include? I've got log statements in the SI, and I'm not seeing them. It feels like it's something obvious I'm just not seeing

The CS:

function onLoad() {
    var userID = g_user.userID;
    g_form.addInfoMessage('userID = ' + userID);
	
    var ga = new GlideAjax('DOCGroupMember');
    ga.addParam('sysparm_name','returnGroups');
    ga.addParam('sysparm_type', '4785e3446fa2310026da31012e3ee4c1');
    ga.addParam('sysparm_user', userID);
    ga.getXML(response1);

    function response1(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage('Answer = ' + answer);
        g_form.setValue("doc_sn_group", answer[0]);
        g_form.setReadOnly('doc_sn_group', true);
    }

The SI:

var DOCGroupMember = Class.create();
DOCGroupMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    returnGroups : function() {
		
		gs.log('RL-DOCGrpmember called');
       
        var gpt = this.getParameter('sysparm_type');
        var usr = this.getParameter('sysparm_user');
		gs.log('RL-DOCGrpmember usr = ' + usr);
		gs.log('RL-DOCGrpmember gpt = ' + gpt);
        var gps = [];
		var gpm = new GlideRecord('sys_user_grmember');
        gpm.addQuery('group.type', 'CONTAINS', gpt);
        gpm.addQuery('user', usr);
        gpm.query();

        while (gpm.next()) {
            gps.push(gpm.group);
        }
        return JSON.stringify(gps);
    },
    type: 'DOCGroupMember'
});

I'm clear I'm probably not handling sending the list of groups back to CS correctly, but right now, it looks like the SI isn't even getting called.

Thanks again.

Yep - it was something obvious: Didn't check 'Client Callable'. D'oh!

Yeah your script include needs to be client callable which actually changes the prototype of it, you may have to create a new one check that box before you start popping in code to ensure you're getting the right format, etc.


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

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!