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

Brian Lancaster
Tera Sage

On your group.push do gr.group.getDisplayValue();

Hi Brian thanks for the reply. 

Are there any other changes needed to get that to work?

Changing
groups.push(gr.group);  

to

group.push(gr.group.getDisplayValue();

that unfortunately didn't have success for me. Adding that, the group field seems to not populate anything with that on my catalog item. 

 

It should be group.push(gr.group.getDisplayValue()); Looks like you were missing an end perenties. Note that this code will now work in the Service Portal if you are using it.

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())

              {
                      group.push(gr.group.getDisplayValue());
              }
    g_form.setValue('groups',groups);

}


Still seeing blank groups. 
find_real_file.png

I double checked that table and i do have a display value set

find_real_file.png