Get all groups user is a member of based off of a catalog variable

IT Eric
Tera Contributor

Hi All,

I may be overthinking this but would like some input.

I have a scenario where on a catalog item, I have a reference variable to the sys_user table. Based off of the user that gets selected, i'd like it to display in a Multi Line Box (or maybe there is a better way) to list all of the groups that selected user is a member of (from the sys_user_grmember table)

Here is my current solution and it's almost there but i'm needing some help.

Have two variables

Reference variable called mirror

Multi line box called groups

OnChange catalog script it goes and fetches information from the sys_user_grmember table 

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('groups',groups);

}

I can only get it to output the sysids of the groups (i have set the display name on the table to return the group name, to no luck that hasn't helped)

find_real_file.png

 

Is there a better way in the onchange script to build in getting the values of those sysIDs, or do i need another script to convert them?

Or is there a much better way to accomplish this.

 

Thanks!

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

Ok so I decided to change this a little and go with a client callable script include that will work in both Service Portal, mobile, as well as the standard interface.

1. Created a script include called GroupsMembership. When you go to create it remember to check client callable. script below.

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

    getGroups: function() {
        var groups = [];
        var user = this.getParameter('sysparm_userID');
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', user);
        gr.query();
        while (gr.next()) {
            groups.push(gr.group.getDisplayValue());
        }
        return groups.toString();
    },

    type: 'GroupsMembership'
});

2. Updated client script using glideajax. script below.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var gajax = new GlideAjax('GroupsMembership');
	gajax.addParam('sysparm_name','getGroups');
	gajax.addParam('sysparm_userID', newValue);
	gajax.getXML(getResults);
}
function getResults(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('groups', answer);
}

View solution in original post

8 REPLIES 8

I think there is a typo group Vs groups and few other tweaks

 

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.getDisplayValue());
              }
    g_form.setValue('groups',groups.toString());

}
-Anurag

Thanks for noticing the typo Anurag! I fixed the typo and tried to no luck, i also tried your code and also am getting the same behavior unfortunately 😞 

 

 

Brian Lancaster
Tera Sage

Ok so I decided to change this a little and go with a client callable script include that will work in both Service Portal, mobile, as well as the standard interface.

1. Created a script include called GroupsMembership. When you go to create it remember to check client callable. script below.

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

    getGroups: function() {
        var groups = [];
        var user = this.getParameter('sysparm_userID');
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', user);
        gr.query();
        while (gr.next()) {
            groups.push(gr.group.getDisplayValue());
        }
        return groups.toString();
    },

    type: 'GroupsMembership'
});

2. Updated client script using glideajax. script below.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var gajax = new GlideAjax('GroupsMembership');
	gajax.addParam('sysparm_name','getGroups');
	gajax.addParam('sysparm_userID', newValue);
	gajax.getXML(getResults);
}
function getResults(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('groups', answer);
}

goodness this is fantastic thank you for taking the time to assist me in this! We're still new with serivcenow and all the different ways to do things. 

I really appreciate your time and assistance!