Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

List Field value to Array

Kyle Wiley
Mega Expert

I am having a problem with the values of a List field and getting them into an array.

The List field itself on my form takes on multiple selection options for different Reference items but when I print the value in a background script, it is all print as a string, one value, instead of an array that I can use to do something with EACH of the sys_ids in the List field.   Here is an example of what I am running into:

  • List Field contains 2 reference records, COMMITTEE ONE, COMMITTEE TWO
  • Print/Alert the List Field Value: 7b401a7b4fc2fe00475a4b8e0210c721,dce9eeaf4f38038036cf76601310c795

I have tried the following and it still isn't working.

var queryString = '0623cec74f8c5f005fed650f0310c7ab';

var ar = [];

var gr = new GlideRecord('task');

gr.addQuery('sys_id', queryString);

gr.query();

if (gr.next()) {

var str = gr.list_field.toString();

var splt = str.split(',');

ar.push(splt);

gs.print("Array Value: " + ar); // Output is 7b401a7b4fc2fe00475a4b8e0210c721,dce9eeaf4f38038036cf76601310c795

gs.print("Array Length: " + ar.length); // Output is 1

I also tried:

var str = gr.list_field;

var splt = str.split(',');

That didn't work either, it still prints as one string.

I cannot get the string value of the List field to separate at the comma so that I can use each individual sys_id to dome something else.

Any ideas?

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi Kyle,



Ideally var splt on line 9 is an array, dont push it anywhere.



When you are pushing it in ar you are creating an array of array


-Anurag

View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi Kyle,



Ideally var splt on line 9 is an array, dont push it anywhere.



When you are pushing it in ar you are creating an array of array


-Anurag

That was it!   Thanks!   I figured it was something to do with my coding... I appreciate it!


Cheers Mate!!


-Anurag