how to insert in list collector

Akki1
Tera Contributor

Hi,

I have a list collector variable and I want to auto populate based on a script include which returns comma separated  sys_id of the records to be inserted. How can I use it in client script and update the list collector?  

6 REPLIES 6

Ravi Gaurav
Giga Sage
Giga Sage

Assuming that you have a list collector variable named 'my_list_collector', you can create a client script that runs when the form loads, queries the script include for the comma-separated sys_ids, and sets the value of the 'my_list_collector' variable using the g_form.setValue() method.

Here's an example of how the client script could look like:

 

function onLoad() {
var myListCollector = g_form.getControl('my_list_collector');

// Query the script include to get the comma-separated sys_ids
var sysIds = gs.getSession().getMyScriptInclude().getSysIds();

// Split the sys_ids into an array
var sysIdsArray = sysIds.split(',');

// Loop through the sys_ids and add each one to the list collector
for (var i = 0; i < sysIdsArray.length; i++) {
myListCollector.addOption(sysIdsArray[i], sysIdsArray[i]);
}

// Set the value of the list collector to the sys_ids
g_form.setValue('my_list_collector', sysIdsArray.join(','));
}

 

In this example, the onLoad() function does the following:

Gets a reference to the 'my_list_collector' variable using the g_form.getControl() method.
Queries the script include using gs.getSession().getMyScriptInclude().getSysIds(), which should return a comma-separated string of sys_ids.
Splits the sys_ids into an array using the split() method.
Loops through the sys_ids array and adds each one to the list collector using the addOption() method.
Sets the value of the 'my_list_collector' variable to the comma-separated sys_ids using the g_form.setValue() method.
Please note that the example script assumes that the script include method getSysIds() returns a comma-separated list of sys_ids. You'll need to modify the script to use the correct script include method based on your implementation.

Also, make sure to test the script thoroughly before deploying it to your production environment.

 


Thanks
Ravi
youtube link :- https://youtube.com/@learnservicenowwithravi

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

@Ravi Gaurav Thank you very much

Also i would like to know if this is necessary 

for (var i = 0; i < sysIdsArray.length; i++) {
myListCollector.addOption(sysIdsArray[i], sysIdsArray[i]);
}

I could work without it

 

Also the comma seperated string I'm getting the first value I'm using to set another reference field field so how can I remove that from array and also the list collector field called co-owners . For it I want to initially take values from form if not empty and append it along with the sys_id I get from the script include along with it. 

currently I'm directly setting the value so its erasing any value manually added by user but I want to keep as it is and add it