how to auto populate list collector value based on values selected in another list collector

Kumar Nandan
Tera Contributor

Hi All

 

I have two list collector variables on a catalog form referencing the same table (u_sap_company_code). Based on selection of first list collector values (Select Company Code), auto population of 2nd list collector(Ad Group Name) values  is required.

I am not sure how to get this achieved so please guide me to get this done.

 

List collector Variables

KumarNandan_0-1700495903761.png

 

u_sap_company_code Table

 

KumarNandan_1-1700495979200.png

 

 

Regards

Nandan

2 REPLIES 2

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @Kumar Nandan 

You can write  onChange Script and get the related AD group using GlideAjax.



Thanks and Regards,

Saurabh Gupta

Brad Bowman
Kilo Patron
Kilo Patron

There is a fundamental issue with your requirement.  A List Collector variable is always going to store as it's value a comma-separated list of sys_ids for the records selected from the referenced table.  A List Collector variable is always going to Display the display column of that table.  Following your example, it appears the Display column for the u_sap_company_code table is the SAP Company Code column.  We can set the value of the second variable to the same value / list of sys_ids as the first variable, but it will display the same values as the first variable.

 

A workaround to show the AD Group name text for each SAP Company Code selected is to first change the second variable to a single or multi-line text type, then create an onChange Catalog Client script when the company code variable changes.  Your script needs to use GlideAjax to call a Script Include, passing in the newValue.  The Script Include will simply do a GlideRecord query on the u_sap_company_code table with a line like

 gr.addQuery('sys_id', 'IN', this.getParameter('sysparm_ids');

where sysparm_ids is the parameter you sent in from the client.  In the while loop for each record returned, push the name column to an array then return the array joined by a comma and a space.  Back in the client script, set the value of the text second variable to the answer returned from the SI.  This list will get rebuilt with each selection, ensuring it stays in sync as records are added and removed from the first variable.

 

Here's an excellent guide on GlideAjax if you're not familiar

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Give this a shot then post your scripts using the insert code </> icon if you get stuck.