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

Funny thing is "getUserByID" actually works in a scoped app, even though the docs say it does not.  That could be new in Vancouver, not sure how far back it works.

 

And a scoped app is a scoped app whether it's meant for internal use only or for sale/distribution through the Store or not, so I'm not sure what you meant by that statement.

I don't know what SN instance you have, but in my PDI running

var x = gs.getUser().getUserByID('abel.tuter').getMyGroups();

in Scripts - Background in a private scope returns

Evaluator: com.glide.script.RhinoEcmaError: Cannot find function getUserByID in object com.glide.script.fencing.ScopedUser@12f41f9.

This in Vancouver patch2-hotfix1.

 

Sure

var x = gs.getUser().getMyGroups();

works, but here one needs

var x = gs.getUser().getUserByID('abel.tuter')...

which does not.

 

The method is only documented for global scope too.
Plus there are a ton of questions everywhere why it does not work in scopes and answers to those that it is not supported in scoped.

 

As for why it is important that an application is for Store or not is because if it is for Store one cannot use the global shimming technique - the solution would fail the review/assessment to authorize its distribution in Store.

Weird, I get the same errors as you in both the Scripts - Background page and Xplore, BUT it does in fact work as a Reference Qualifier (Vancouver patch2-hotfix1-10-04-2023).  I have the variable setup in the same scope as I'm trying in Scripts - Background and Xplore.

Perhaps because portal runs a lot of stuff in the background actually in global scope?