- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 10:55 AM
I have a string array that I would like to pass to a list collector variable. Unfortunately it is only setting the last object in the array. How can I make it populate the entire array?
Script:
var seperate = answer.split(','); // "answer" is a comma seperated string
for (var i = 0; i < seperate.length; i++) {
g_form.setValue("test_list_collector_variable", seperate[i]);
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:30 PM
Hi,
What does "it didn't work" mean...because in this scenario, it wouldn't be the same reason that you've mentioned above.
I have no idea what your "answer" actually is...so please verify that.
Then...if it's a array of separated values, as you've mentioned, using:
seperate.join(',')
does in fact work, otherwise, if it's string from a script include that you're getting, for example, you can use:
g_form.setValue('field_name', answer.toString());
Again, as long as it's a comma separated list of sys_ids relevant to that field (reference is the right table and the values are sys_ids).
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:30 PM
Hi,
What does "it didn't work" mean...because in this scenario, it wouldn't be the same reason that you've mentioned above.
I have no idea what your "answer" actually is...so please verify that.
Then...if it's a array of separated values, as you've mentioned, using:
seperate.join(',')
does in fact work, otherwise, if it's string from a script include that you're getting, for example, you can use:
g_form.setValue('field_name', answer.toString());
Again, as long as it's a comma separated list of sys_ids relevant to that field (reference is the right table and the values are sys_ids).
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:37 PM
"Didnt work" means that it is giving me the same result as my initial entry. Only one of the objects in the array is being stored in the list collector variable. Nothing has changed in that regard.
if you look at my initial entry, you can see what my "answer" is (got this from g_form.addInfoMessage(answer) 😞
f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13,f6d3cc161bf981106d4954e0604bcb13
I have tried g_form.setValue("test_list_collector_variable", answer); which resulted in the same single object issue. There is no reason to add the toString() after answer because I already converted the array into a string as mentioned in the initial entry.
g_form.addInfoMessage(answer); // This is an array that I already converted to a string via .toString()
var seperate = answer.split(',');
g_form.setValue("test_list_collector_variable", seperate.join(','));
Keep in mind that this is on a Service Portal form.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:46 PM
Hi,
It seems you understand where the issue is, so I'll leave it at that.
Unless you're using "typeof" to see what you're getting, you may not know because...you're using:
var seperate = answer.split(','); // "answer" is the string array
.split() - which is taking the string from answer...and putting it into an array...which is not appropriate for setting a list field, hence why I am suggesting what I did: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split feel free to read that over.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:54 PM
"It seems you understand where the issue is" - How so?
I copied your solution exactly and it resulted in the same issue.
g_form.addInfoMessage(answer); // This is a comma separated string
g_form.setValue("test_list_collector_variable", answer);
also resulted in the same issue, so I'm not sure what you are suggesting.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:30 PM
Hi,
I tried the similar thing for my watch list field and it is working.
var gr = new GlideRecord('incident');
gr.addQuery('sys_id','4715ab62a9fe1981018c3efb96143495')
gr.query();
if(gr.next())
{
var answer = '62826bf03710200044e0bfc8bcbe5df1,a8f98bb0eb32010045e1a5115206fe3a,0a826bf03710200044e0bfc8bcbe5d7a';
var seperate = answer.split(','); // "answer" is the string array
for (var i = 0; i < seperate .length; i++) {
gr.watch_list = seperate.join(',')
}
gr.update();
}
TTry using gr.test_list_collector_variable= seperate.join(',') instead of setValue
Regards,
Deepankar Mathur