How to get values from list using ForEach?

sowmyaj
Giga Expert

Hi,

I have a list, by default it will fetch comma separated values, but I want values of list to be line separated not comma separated.

Example,

list is: abc,xyz,mno

result= abc,xyz,mno

expected result= abc

                                                      xyz

                                                      mno

In my table I have a field of type 'list' which takes multiple values. I am fetching the records and displaying it in my UI page.

The field name is u_team. When I print it using $[u_team], the result is displayed with comma separated, e.g. abc,xyz,mno

I tried using forEach but I didn't get the expected result.

I tried with for loop in client script of my Ui page, it worked fine but am not able to do that in HTML using forEach.

Client Script Code: (works fine)

function userList(names){

alert(names);
var array=names.split(",");
for (var i=0; i < array.length; i++) {
      alert(array[i]);
 
  }
}

How can I achieve this?

Thanks,

Sowmya

1 ACCEPTED SOLUTION

Rushit Patel2
Tera Guru

HI Sowmya,



see below code. it is in phase1 but you can replace g to g2, j to j2 and {} to [] for phase2.



<g:evaluate>


  var gr = new GlideRecord('incident');


  gr.get('e8caedcbc0a80164017df472f39eaed1');


  var list = gr.u_todo.getDisplayValue(); <!-- todo is a list field     -->


  var one = list.split(',');



  </g:evaluate>




  <j:forEach var='jvar_item' items='${one}'>


  <h3>


  ${jvar_item}


  </h3>


  </j:forEach>





let me know if this works.


View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Sowmya,



Could you post the code you used for your ui page? If you have your value as a comma separated list in the jelly, you could just run a javascript replace() to replace the commas with html line breaks (<br/>).


Rushit Patel2
Tera Guru

HI Sowmya,



see below code. it is in phase1 but you can replace g to g2, j to j2 and {} to [] for phase2.



<g:evaluate>


  var gr = new GlideRecord('incident');


  gr.get('e8caedcbc0a80164017df472f39eaed1');


  var list = gr.u_todo.getDisplayValue(); <!-- todo is a list field     -->


  var one = list.split(',');



  </g:evaluate>




  <j:forEach var='jvar_item' items='${one}'>


  <h3>


  ${jvar_item}


  </h3>


  </j:forEach>





let me know if this works.


Hi Rushit,



        Thanks a lot.   It's Working



Thanks,


Sowmya