The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Populate List Collector Variable from String Array (Service Portal)

JJG
Kilo Guru

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]);


}

 

1 ACCEPTED SOLUTION

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!

View solution in original post

13 REPLIES 13

Allen Andreas
Administrator
Administrator

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!

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(','));

 

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!

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(','));