Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

My Change Client Script doesn't populating expected output

imranshaik0
Tera Contributor

Hi Community!

Hope you are fine!


I am having a requirement and that was quite simple.
While learning new about catalog items and behaviour, I stuck with this issue.

In a catalog item, I want to populate the groups for which the opened for is the group manager. For that I have created my own script. But somehow, it is failing. I need your help here to sort this out. Please check my script. I have used GlideAjax and Script Include for this. (Suggestions are accepted on which approach can be best for better efficiency and platform health)

My ScriptInclude: getUserGroupsForCatalog

Glide AJAX enabled : Checked

Script: 

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

userGroups: function() {
var allGrps = [];
var user = this.getParameter('sysparm_user');
var grp = new GlideRecord('sys_user_group');
grp.addQuery('manager', user);
grp.query();
while (grp.next()) {
allGrps.push(grp.sys_id);
}
return JSON.stringify(allGrps);

},
type: 'getUserGroupsForCatalog'
});

and My onChange Catalog scripts:
Name: To show User groups
Variable Name: opened_for
Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var requestedFor = g_form.getValue('opened_for');
    var ga = new GlideAjax('getUserGroupsForCatalog');
    ga.addParam('sysparm_name', 'userGroups');
    ga.addParam('sysparm_user', requestedFor);
    ga.getXMLAnswer(function(response) {
        if (response) {
            var output = JSON.parse(response);
            if (output.length > 0) {
                g_form.setValue('user_groups', output.join(','));
            } else {
                g_form.addErrorMessage('User is not the manager of any groups');
            }
        } else {
            g_form.addErrorMessage('User is not the manager of any groups');
        }
    });

}
While this was written for onChange, I also wanted to show the groups when the catalog item is loaded too. Please Suggest me answers. Thank you.

 

 

2 REPLIES 2

lpruit2
Kilo Sage

Greetings @imranshaik0 ,

 

Can you try replacing grp.sys_id with grp.getUniqueValue() within your push statement within the Script Include?

 

Also, would you be interested in clearing the value of the "opened_for" either BEFORE you set any value or within an Else statement if the passed in user is not a manager of any groups? That way, if the user changes the value of the "opened_for" field more than once, you won't be stacking or aggregating the groups of the second change on top of the first change in value.


Finally, I'll include a link to the always popular GlideAjax Cheat Sheet which always helps me in these situations. I hope this information helps!


GlideAjax Example Cheat Sheet (UPDATED) - ServiceNow Community


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

    userGroups: function() {
        var allGrps = [];
        var user = this.getParameter('sysparm_user');
        var grp = new GlideRecord('sys_user_group');
        grp.addQuery('manager', user);
        grp.query();
        while (grp.next()) {
            allGrps.push(grp.getUniqueValue());
			//allGrps.push(grp.sys_id);
        }
        return JSON.stringify(allGrps);

    },
    type: 'getUserGroupsForCatalog'
});

 

imranshaik0
Tera Contributor

Hi @lpruit2 ,

Thanks for the information. 

I have tried this too before but no luck.

But somehow I managed to solve it.

I used Advanced Reference Qualifier with the below condition and it worked.
"javascript: 'manager=' + current.variables.requested_by"

Enhancements and Best practices still accepted.

Thanks again.