Jelly Array

Ken83
Mega Guru

Hello Community,

I'm hoping this is a simple issue to resolve but I just can't figure it out. I am running a GlideRecord to a table that contains 11 records. I want to return those 11 records and list them on a page as HTML elements. Simple enough, yeah? So here is the code that I am using...

<g:evaluate var="jvar_core_choices" object="true">

var productArray = new Array();

var gr = new GlideRecord('u_product_line');gr.orderBy('u_core_product'); gr.query(); gr;

while(gr.next()){

          productArray.push(gr.u_core_product);

  }

productArray;

</g:evaluate>

<j:forEach items="${jvar_core_choices}" var="jvar_array_item">

    <h3>${jvar_array_item}</h3>

</j:forEach>

With this code, I am able to return 11 results and print 11 elements to the screen. The problem is that all 11 elements have the same values. Which is incorrect. These should be 11 different items. Here is a screenshot of what I'm talking about...

2015-08-04 12_34_31-ServiceNow Service Automation.png

Any ideas why it's not printing the correct values?

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Kenneth,



This is a simple problem. When you do the Array.push, you are pushing a GlideElement (an object) into the array. When you do gr.next(), the values in each of the GlideElements that make up the fields on the GlideRecord get updated, but the objects themselves stay the same.



Each time you push to that array, you are storing a reference to the GlideElement, not pushing the value it contains. When you then loop over the array, you are accessing the GlideElement 11 times, and that GlideElement holds the value from the alst record you nexted to.



The solution is to use getValue('field_name'):



<g:evaluate var="jvar_core_choices" object="true">  


var productArray = new Array();  


var gr = new GlideRecord('u_product_line');        


gr.orderBy('u_core_product');


gr.query();



while(gr.next()){  


          productArray.push(gr.getValue('u_core_product'));  


  }  


productArray;


</g:evaluate>  


 


<j:forEach items="${jvar_core_choices}" var="jvar_array_item">  


    <h3>${jvar_array_item}</h3>  


</j:forEach>



Now you are storing the value at each iteration, and your loop will show the correct values.


View solution in original post

3 REPLIES 3

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Kenneth,



This is a simple problem. When you do the Array.push, you are pushing a GlideElement (an object) into the array. When you do gr.next(), the values in each of the GlideElements that make up the fields on the GlideRecord get updated, but the objects themselves stay the same.



Each time you push to that array, you are storing a reference to the GlideElement, not pushing the value it contains. When you then loop over the array, you are accessing the GlideElement 11 times, and that GlideElement holds the value from the alst record you nexted to.



The solution is to use getValue('field_name'):



<g:evaluate var="jvar_core_choices" object="true">  


var productArray = new Array();  


var gr = new GlideRecord('u_product_line');        


gr.orderBy('u_core_product');


gr.query();



while(gr.next()){  


          productArray.push(gr.getValue('u_core_product'));  


  }  


productArray;


</g:evaluate>  


 


<j:forEach items="${jvar_core_choices}" var="jvar_array_item">  


    <h3>${jvar_array_item}</h3>  


</j:forEach>



Now you are storing the value at each iteration, and your loop will show the correct values.


Kalaiarasan Pus
Giga Sage

As stated above, using the getValue function should resolve it....


bernyalvarado
Mega Sage

Hi Kenneth, Cory Seering explanation nails it.



Perhaps just to enrich this thread, doing a .toString() cast will also do the trick:



productArray.push(gr.u_core_product.toString());




Thanks,


Berny