How to display values using of an array using for loop

jenny32
Tera Guru

Hi All,

In my table there is a field "Team" of type "List". I'm selecting more than one member in that field. Now I want to display the values of that filed one after another.

code:

<td>$[escalation.u_team.getDisplayValue()]</td>

When I try to display the values using the above code, I got the result as "Quintin Isacson, Waldo Edberg".

I need the values to be displayed in the below format:

Quintin Isacson

Waldo Edberg

I used foreach tag but it is not working.

Here is the code using "foreach"

<tr>
  <td class="a2_td">
    <g:evaluate>
    var list = escalation.u_team.getDisplayValue();
        <!--var array = list.split(","); -->
               
    </g:evaluate>
      <j:forEach items="${list}" var="jvar_array">
        <g:evaluate jelly="true">  
                      var name = jelly.jvar_array;  
              </g:evaluate>  
        <p>  
                      User: $[SP] <span>${name}</span>  
              </p>  
      </j:forEach>  

          </td></tr>

Any clue on how to make this code work?

Thanks,

Jennifer

6 REPLIES 6

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Jennifer,



You might find this answer helpful:



Loop thought list collector



Of course, you will need to adapt the script to jelly.



Regards,


Sergiu


Kalaiarasan Pus
Giga Sage

u_team.getDisplayValue() will give a comma separated list of display values.. Split using ',' and loop through the resulting array.


Hi Kalai,


I did the same way but it is not working. When I try to display the values of array using ForEach I'm getting blank column and values are not displayed.



Here is the part of code where I use ForEach in HTML part of my UI page:


<j:jelly>


<g2:evaluate>


var escalation=new GlideRecord('u_escalation_level');


escalation.orderByDesc('u_level');


      escalation.query();


</g2:evaluate>


<g2 height="200px">


  <table class="a2_table">


  <tr>


    <th class="a2_th" width="100%"><h4><b>Escalation Level</b></h4></th>  


  </tr>


</table>


<table class="a2_table">


<tr>


        <th class="a2_th"><h4><b>Team</b></h4></th>


</tr>


 


  <j2:while test="$[escalation.next()]">


 


      <tr>


  <td class="a2_td">


    <g:evaluate>


    var list=escalation.u_team.getDisplayValue();


var array=[];


          array = list.split(",");


      </g:evaluate>


      <j:forEach items="${array}" var="jvar_users">  


      <g:evaluate jelly="true">    


                          var name = jelly.jvar_users;


                </g:evaluate>  


              <p>  


               


      User: $[SP} <span>${name}</span>


              </p>  


      </j:forEach>  


        </td>


  </tr>


      </j2:while>


  </table>


</g2>


</j:jelly>



Thanks,


Jenny


jancaasi
Mega Expert

Hi Jennifer,



Have you tried alerting the array[0] after you split to check if there is a value in it?