How to set the script include filter values in Onchange clinet script Reference variable ?

raj99918
Tera Contributor

Hi,

 

Am calling script include and getting the filter values as well but not able to set those values in OnChnage client script of Reference(Groups table) variable.

Below is my Onchange script please let me know what.s the issue here?

-----------------------

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var gajax = new GlideAjax('Groupsdata');
    gajax.addParam('sysparm_name', 'getGroupnames');
    gajax.addParam('sysparm_userID', newValue);
    gajax.getXML(getResults);
}
function getResults(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
var user = g_form.getValue('requested_for');
    if (user != '') {
         alert(answer);
         var groupSysIds = answer.split(',');
g_form.setValue('assignment_group', groupSysIds.join(','));
}
}
1 ACCEPTED SOLUTION

@raj99918 Please update the script include script as follows.

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

    getGroupnames: function(user) {
        var groups = [];        
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', user);
        gr.query();
        while (gr.next()) {
            groups.push(gr.group.sys_id);
        }
        return 'sys_idIN' + groups.toString();         
    },
type: 'Groupsdata'
});

Also, apply the same reference qualifier again on the Group field in order to make the reference qualifier call the script include.

View solution in original post

8 REPLIES 8

Sandeep Rajput
Tera Patron
Tera Patron

@raj99918 You can only set one value on a reference field whereas your script is try to set a bunch of sys_ids as values. 

 

It looks like you are trying to restrict your reference field to certain values. This is ideally achieved through reference qualifiers and not using onChange script. I recommend you go through the documentation on reference qualifier here https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/script/server-scripting... and an example here https://www.servicenow.com/community/itsm-articles/reference-qualifier/ta-p/2306509

 

Hope this helps.

Hi @Sandeep Rajput  Thanks for your inputs. Yes am trying to restrict the refernce qualifer but when I used this Reference qualifer and it's not working for me below is my qualifer:

 

javascript: new x_gro_rc_sm.Groupsdata().getGroupnames(current.variables.requested_for.toString());

@raj99918 Please share your Script include code. 

@Sandeep Rajput 

 

SI Code:

 

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

    getGroupnames: 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.sys_id);

        }
        //return 'sys_idIN' + groups.toString();
         return groups.join(',');
    },
type: 'Groupsdata'
});
 
Note: Client callable is true and it's running on scoped application