To populate list collector values based on another select box variable

Koyel Guha
Tera Contributor

Hi,

I am trying to populate the results in list collector variable based on the value selected from another variable.

I have variable A (selectbox) and variable B(list collector). When I select any option from variable A for eg. "x1" then variable B referencing a customized table having a field u_description which will filter u_description contains "read" keyword and u_description contains "x1". Based on the condition, it will display the one or multiple results from the table in the varible B.

 

I am using a script include and client script for this requirement and it is not working. Can somebody help on this.

 

Script Include

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

    getFilteredSysIds: function() {
        var input = this.getParameter('sysparm_id');
        var info = [];
      var gr = new GlideRecord('u_active_directory_groups');
      gr.addEncodedQuery('u_descriptionLIKEread^u_descriptionLIKE'+ input);
    gr.query();
        while (gr.next()) {
        // info.push({
        //  "label": gr.u_query_name.toString(),
        //  "value": gr.u_query_name.toString()

        info.push({
         "label": gr.getValue('u_query_name'),
         "value": gr.getValue('u_query_name')
});
  
        }
    return info;
    //   return json.encode(info);
    },
     type: 'FilterActiveDirectory'
});
 
Client script
 function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        //g_form.clearValue('please_select_the_following');
     return;
    }
    var ga = new GlideAjax('FilterActiveDirectory');
    ga.addParam('sysparm_name', 'getFilteredSysIds');
    ga.addParam('sysparm_id', g_form.getValue('please_select_the_fileserver_where_the_shared_folder_is_located'));
    // ga.getXMLAnswer(info);
    // function info(answer) {
    // var msgObj = JSON.parse(answer);

    ga.getXMLAnswer(function(response) {
    var msgObj = JSON.parse(response);
g_form.clearOptions('please_select_the_following');
g_form.addOption('please_select_the_following', '', '--None--');
for (var i = 0; i < msgObj.length; i++) {
g_form.addOption('please_select_the_following', msgObj[i].value, msgObj[i].label);
}
//}
 //}

    });
}
2 ACCEPTED SOLUTIONS

@Koyel Guha 

you need to do 2 things which I mentioned in my 1st response to your question

AnkurBawiskar_0-1751359526323.png

 

1) advanced ref qualifier

a) why variable value will be random? You said it's a select box so it should have choices in it, you can use that.

You can use this, for x1 choice value you can have a query

javascript&colon;var variableValue = current.variables.please_select_the_fileserver_where_the_shared_folder_is_located.toString() ; var query; if(variableValue == 'x1') query = 'u_descriptionLIKEread^u_descriptionLIKE' + variableValue; query; 

If you want the same filter for other drop down choices then do this generic

javascript&colon;var variableValue = current.variables.please_select_the_fileserver_where_the_shared_folder_is_located.toString() ; var query = 'u_descriptionLIKEread^u_descriptionLIKE' + variableValue; query; 

2) variable attributes - add this

ref_qual_elements=please_select_the_fileserver_where_the_shared_folder_is_located 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

@Koyel Guha 

please update : in your ref qualifier.

Community converts : to &colon;

share the choices for your select box?

Also share if there are records matching in your table with that filter condition?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

20 REPLIES 20

Rushi Savarkar
Mega Sage

Hello @Koyel Guha 

Hope you are doing well!

 

Your requirement looks like based on the group, populate the group members. Here, the Group field will select box field(reference field), and the Group member field will be a List collector field. 

 

Please find below the script include and call the script include from your list collector type variable, and change it according to your requirement:

var displayData = Class.create();
displayData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
showGroupmembers: function(groupID) {
        var memArray = [];
		var grup = groupID;
        //var groupID = this.getParameter("sysparm_group");
        var grMem = new GlideRecord("sys_user_grmember"); //change the table name 
        grMem.addQuery('group', grup); // you can add your "read" keyword logic here
        grMem.query();
        while (grMem.next()) {
            memArray.push(grMem.getValue("user").toString());
        }
        return 'sys_idIN' + memArray.join(",");
    },

    type: 'displayData'
});

Below is the reference qualifier condition on the List Collector type variable:

Here, group_name is the database name of Group field

javascript&colon; new global.displayData().showGroupmembers(current.variables.group_name);

 Output:

RushiSavarkar_0-1751209654108.png

After doing all this stuff, you can use an onchange client script to clear the value on change of the Group field

Thank you!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

Hi Rushi,

It is still not working for me. It is displaying all the data in the list collector variable.

SI : 

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

    getFilteredSysIds: function() {
        var input = this.getParameter('sysparm_id');
        //input = input.replace(/[^a-zA-Z0-9 ]/g, "");
   
        var info = [];
   
       

      var gr = new GlideRecord('u_active_directory_groups');
      gr.addEncodedQuery('u_descriptionLIKEread^u_descriptionLIKE'+ input);
    gr.query();
        gs.log("Koyel12" + gr.getEncodedQuery());
       

        gs.log("Koyel" + input);

        while (gr.next()) {
 gs.log("Koyel19" + gr.u_query_name.getValue());

       //info.push(gr.getValue('u_query_name'));
   
    info.push(gr.getValue('u_query_name').toString());
   
        // info.push({
        //  "label": gr.u_query_name.toString(),
        //  "value": gr.u_query_name.toString()

        // info.push({
        //  "label": gr.getValue('u_query_name'),
        //  "value": gr.getValue('u_query_name')
//});
           

        }
    return info;
    //return 'sys_idIN' + info.join(",");
    //   return json.encode(info);
     // return info.join('!');
    //return JSON.stringify(info);
    },
     type: 'FilterActiveDirectory'
});
 
ref qual: 
javascript&colon; new global.FilterActiveDirectory().getFilteredSysIds(current.variables.please_select_the_following);
 
Can you tell me where I am doing a mistake. 
 
Thanks.

@Koyel Guha 

Did you get a chance to check my earlier response?

You should be able to achieve your requirement using that.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello @Koyel Guha 

Please find below the code, which may help you to resolve your problem

var displayData = Class.create();
displayData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
displayKeyword: function(inp) {
        var memArray = [];
        var input = inp;
        var grMem = new GlideRecord("u_jurisdiction");
        grMem.addQuery("u_juri_nameLIKE"+ input +"^ORu_juri_nameLIKE" + input); //I used OR operator
        grMem.query();
        while (grMem.next()) {
            memArray.push(grMem.getValue("sys_id").toString());
        }
        return 'sys_idIN' + memArray.join(",");
    },
    type: 'displayData'
});

Below is the ref qualifier: 

 

javascript&colon; new global.displayData().displayKeyword(current.variables.variable_a);

 

Here, Variable A is a select box variable. I have custom choices in this field

Output:

RushiSavarkar_0-1751291727392.pngRushiSavarkar_1-1751291767041.png

Thank you!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

Hi Rushi, 

its still not working. Not sure where it is getting wrong. It is showing all the options in the list collector variable no matter what I change.

 

var FilterActiveDirectory = Class.create();

FilterActiveDirectory.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getFilteredSysIds: function(inp) {

//   var input = this.getParameter('sysparm_id');

    var input = inp;

       

    var info = [];

      var gr = new GlideRecord('u_active_directory_groups');

    //  gr.addEncodedQuery('u_descriptionLIKEread^u_descriptionLIKE'+ input);

    gr.addQuery('u_descriptionLIKE'+ read + '^u_descriptionLIKE'+ input);

    gr.query();

   

        gs.log("Koyel17" + input);

        while (gr.next()) {

       

        gs.log("Koyel19" + gr.u_query_name.getValue());

   

    info.push(gr.getValue('u_query_name').toString());

        }

   

    return 'u_query_nameIN' + info.join(",");

    },

     type: 'FilterActiveDirectory'

});

 

javascript&colon; new global.FilterActiveDirectory().getFilteredSysIds(current.variables.please_select_the_fileserver_where_the_shared_folder_is_located);

 

Thanks.