How to check the empty of the glide object.

navyag
Kilo Contributor

Hi Team,

I am fetching the records from the table, i used if condition to check the emptiness of the glide object if the object contain the record , the if condition will run and it skip the first record of the glide object.

Find the below snippet which we have used to fetch the data and checking the emptiness of the object.

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

  var gr = new GlideRecord("table_name");

  gr.addQuery('type','degradation').addOrCondition('condition','a');              

    gr.query();

    gr;

</g:evaluate>

<j:if test="${!jvar_gr.next()}">//when this script runs if the records are present in the object it skipping the first record of the object.

no records;

</j:if>

<j:while test="${jvar_gr.next()}">

//statements

</j:while>

Please give suggestion to avoid the skipping of the first record.

Thanks in advance.

8 REPLIES 8

Brad Tilton
ServiceNow Employee
ServiceNow Employee

What happens when you write it like this?



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


  var gr = new GlideRecord("table_name");


  gr.addQuery('type','degradation').addOrCondition('condition','a');            


    gr.query();


    gr;


</g:evaluate>


<j:if test="${!jvar_gr.hasNext()}">//when this script runs if the records are present in the object it skipping the first record of the object.


no records;


</j:if>


<j:while test="${jvar_gr.next()}">


//statements


</j:while>


For instance, if there 5 are records in the table , it is returning only 4 records, skipping the first record.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

It's because your if next() statement is iterating through the first record, did you replace it with hasNext() as in my example?


I did , but it is not working as expected when there is no record .