- 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 11:14 AM
Hi,
You'd use:
g_form.setValue("test_list_collector_variable", seperate.join(','));
Please also check your script for spacing issues...you have spaces all in your script (that may be a result of your copy/paste, but I'm mentioning it just in case).
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:18 PM
Unfortunately it is still just adding one array object to the list collector.
var seperate = answer.split(',');
g_form.setValue("test_list_collector_variable", seperate.join(','));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 12:25 PM
Hi,
That's because you're iterating through them in a for loop.
You don't need it. Which is why I didn't cover that above.
You can add them in straight away, without looping through and adding them one at a time. When you use setValue, it's only going to set the value of that field to what you tell it. So if you're looping through 15 times, it's not setting 15 values...it's setting 1 value 15 different times, with the last being what the end result would be.
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:26 PM
I got rid of the for loop and it didn't work.
var seperate = answer.split(',');
g_form.setValue("test_list_collector_variable", seperate.join(','));