Catalog Client Script with Script Include to populate a Group select box from a User selection

Jason Thornton
Tera Contributor

Hello community! First time crying for help!

I am working on a service catalog item.

I am trying to use a script include and catalog client script (posted below, adapted from one I found here) and it's not working.

My use case is, the end user selects a user from a reference select box, that pre-populates the owner_asg select box only with groups that user is a member of.

The end user can change the user and the owner_asg box should change available groups to the new users groups.

I'm open to other solutions but thought the script include was the way to go. Any help is appreciated.

 
Script include:
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.groups.getDisplayValue());
        }
        return groups.toString();
    },

    type: 'GroupsMembership'
});

 

Catalog Client Script:

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('owner_asg', answer);
}
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Jason Thornton Here is the solution we finally concluded.

 

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

    getGroups: function(record) {
        gs.info('in script')
        var groups = [];
        var user = this.getParameter('sysparm_userID');
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', record);
        gr.addQuery('group.name','STARTSWITH','ASG');
 

        gr.query();
        while (gr.next()) {
            groups.push(gr.group.sys_id);
            gs.info(gr.group.sys_id)
        }
        gs.info("record is" + record);
        return 'sys_idIN'+groups.toString();
    },
 

    type: 'GroupsMembership'
});
GroupsMembership.get = function() {
    return new GroupsMembership();
};

 

Here is the reference qualifier.

 

//Variable Reference Qualifer Script
javascript: x_scopapp_utils.GroupsMembership.get().getGroups(current.variables.user);

View solution in original post

13 REPLIES 13

No joy. Does it matter what my variable name is for the user selection?

Also, do I need to declare the variable on the header of the catalog client script? Thanks!

Jason Thornton
Tera Contributor

This was resolved offline. Thanks to Sandeep for the assist.

Sandeep Rajput
Tera Patron
Tera Patron

@Jason Thornton Here is the solution we finally concluded.

 

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

    getGroups: function(record) {
        gs.info('in script')
        var groups = [];
        var user = this.getParameter('sysparm_userID');
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', record);
        gr.addQuery('group.name','STARTSWITH','ASG');
 

        gr.query();
        while (gr.next()) {
            groups.push(gr.group.sys_id);
            gs.info(gr.group.sys_id)
        }
        gs.info("record is" + record);
        return 'sys_idIN'+groups.toString();
    },
 

    type: 'GroupsMembership'
});
GroupsMembership.get = function() {
    return new GroupsMembership();
};

 

Here is the reference qualifier.

 

//Variable Reference Qualifer Script
javascript: x_scopapp_utils.GroupsMembership.get().getGroups(current.variables.user);

Jim Coyne
Kilo Patron

FYI, that can be solved without a Script Include, which is typically an easier/cleaner solution.  Too many jump to Script Includes as a solution to advanced reference qualifiers.

 

Setup your group variable as a Lookup Select Box with an attribute of "ref_qual_elements=crep_owner".  This tells SN to send back the value of that variable to the server for use in the Reference Qualifier (although in Vancouver it does not seem to be required anymore).

 

Then you set the Reference qualifier field to be:

javascript: var user = gs.getUser().getUserByID(current.variables.crep_owner);
var groups = user.getMyGroups().toArray().join(",");
var reference = "active=true^sys_idIN" + groups;
reference;

 

JimCoyne_1-1706250114157.png

 

And so you end up with this:

 

JimCoyne_2-1706250225467.png

 

And the list of Groups is updated based on the selected User:

 

JimCoyne_3-1706250294984.png

 

The Reference qualifier can also be "simplified" with chaining down to:

javascript:"active=true^sys_idIN" + gs.getUser().getUserByID(current.variables.crep_owner).getMyGroups().toArray().join(",");

 

This will not work here like that as getUserByID is not supported in scopes.

 

But if this is for internal use only (not meant to be published in Store), I would still build the solution around getUserByID even if it means I would need to wrap it in a global Script Include, because:

- it honors group hierarchy (returns parent groups the user is member of)

- it does not include not-active groups.

 

But if group hierarchy is not needed and only direct memberships are to be considered, this can be done even simpler (at least in my opinion) than all the solutions above:

 

javascript∶ 'SUBQUERYsys_id,group,sys_user_grmember^user=' + current.variables.crep_owner + '^ENDSUBQUERY^active=true'

 

 

*) Note that above the semicolon after keyword javascript is not actually a semicolon (but the Unicode ratio character), so you have to replace it with an actual semicolon if you copy-paste the whole reference qualifier.