Jelly Script IF the n condition ELSE then While loop

lomouhamadou
Kilo Guru

Hi all,

I have this jelly script below where we queried a table and displays all records. It is working so and fine.

I want to add the option to display "No record found" in case the query is empty.

I tried to add an if but does not work.

Can anyone help me with this script by adding the lines that will check if the query is empty and set a message to be displayed.

This is a ui page.I have a icon on HR case which onclick called the UI macro which will render the result on an Iframe.

//Script below

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

      <g:evaluate var="jvar_opened_for" expression="RP.getWindowProperties().get('opened_for')" />

      <g:evaluate var="jvar_employee_language" expression="

                                                                              var sys_user = new GlideRecord('sys_user');

                                                                              sys_user.get(RP.getWindowProperties().get('opened_for'));

                                                                              sys_user.u_employee_language;

                                                                              " />

     

      <div id="internal_messages_list">

                <ol>

              <g2:evaluate var="jvar_item" expression="

                                                      gr = new GlideRecord('u_internal_message');

                                                      gr.addQuery('u_user',RP.getWindowProperties().get('opened_for'));

                                                      gr.addQuery('u_start_date', '&lt;=', gs.daysAgo(0));

                                                      /*gr.addQuery('u_expiration_date', '&gt;=', gs.daysAgo(0));*/

                                                      gr.query();

                                                      " />      

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

                                                      <li><p><g2:no_escape>$[NS:gr.u_message]</g2:no_escape></p></li>

              </j2:while>              

     

              </ol>

      </div>

     

     

</j:jelly>  

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

On the line before your while loop you could do this:



<j2:if test="$[!gr.hasNext()]">


      No records


</j2:if>


View solution in original post

6 REPLIES 6

Chuck Tomasi
Tera Patron

Try this before your j2:while.



<j2:if test="gr.getRowCount() == 0" >


        No records found<br />


</j2:if>


Nope. not working


Sorry about that. I forgot my $[ and ] to make that actually work.



Thanks for picking up the slack Brad!


Hi @Chuck Tomasi , @Brad Tilton ,

 

I need your help very urgent....I have below script which always satisfying if condition or may be not working even though users dont have appropriate roles while showing the buttons on UI Macro which is used in workplace portal widget . Below is my code in ui macro.

 

Please suggest.

 

 

<g:evaluate>
 
 
  var checkRole=gs.hasRole('admin')||!gs.hasRole('sn_wsd_rsv.selectRooms'); 
 
 
</g:evaluate>
 
 
<div ng-if="!displayRsvCard &amp;&amp; isNative"></div>
 
 
 
<div class="schedule-col col-right text-center col-xs-1" ng-if="displayRsvCard">
<div class="reservable-action" tabindex="0" role="gridcell">
 
 
<j2:if test="$[checkRole]">
<button type="button" id="add-{{item.sys_id}}" class="btn btn-default btn-add btn-reserve form-control text-overflow-ellipsis"
ng-class="{'btn-remove': item.is_selected, 'btn-reserved': reservable.is_reserved, 'btn-selected': reservable.is_selected}"
ng-disabled="!item.is_available || (disableReserveMulti &amp;&amp; !item.is_selected)"
tabindex="-1"
aria-label="{{item.is_selected ? item.deselect_label : item.select_label}}"
ng-attr-title="{{item.is_selected ? scheduleViewLabels.remove : scheduleViewLabels.add}}"
ng-click="toggleReservableSelection({item: item, event: $event})">
<label class="btn-label-reserve">{{item.is_selected ? scheduleViewLabels.remove : scheduleViewLabels.add}}</label>
<i ng-class="!item.is_selected? 'fa fa-shopping-cart btn-icon-reserve' : 'fa fa-trash btn-icon-reserve'"></i>
</button>
</j2:if>
</div>
 
</div>

 

 

Thanks in Advance,