UI Macro/g:evaluate help

matthew_magee1
Giga Guru

Hi all,

I'm trying something new (for me). I've got a UI Macro that I'm modifying.

I've added the following g:evaluate tag:

<g:evalute var="cart_count" object="true">

var cart_count = new GlideAggregate('sc_cart_item');

cart_count.addAggregate('SUM','quantity');

cart_count.addQuery('active','true');

cart_count.addQuery('sys_created_by',gs.getUserName());

cart_count.setGroup(false);

cart_count.query();

cart_count;

</g:evaluate>

The above code will give me a quantity for the number of catalog items I have in a shopping cart for the current user. This works great.

Later in the code, I have this <j:if> statement:

<j:if test="${cart_count.next()}">${cart_count.getAggregate('SUM','quantity')}</j:if>

The above code works great if all I want to show is how many items a user has in his/her cart.

What I'd like to do is throw an if statement that checks if the cart quantity for a user is 0:

if (SUM == 0/null) {

display '0'

}

else {

${cart_count.getAggregate('SUM','quantity')}

}

Obviously I have the 2nd half working, just not sure how to do the 1st half. Any help is greatly appreciated-

Matthew

1 ACCEPTED SOLUTION

This worked for me:



<?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 jelly="true">        


  var cart_count = new GlideAggregate('sc_cart_item');        


  cart_count.addAggregate('SUM','quantity');        


  cart_count.addQuery('active','true');        


  cart_count.addQuery('sys_created_by',gs.getUserName());        


  cart_count.setGroup(false);        


  cart_count.query();


  </g:evaluate>  


  <j:if   test="${cart_count.next()}">


  <j:choose>


  <j:when test="${cart_count.getAggregate('SUM','quantity')} != 0">


  Number of items:         ${cart_count.getAggregate('SUM','quantity')}


  </j:when>


  <j:otherwise>


  Number of items:         0


  </j:otherwise>


  </j:choose>


  </j:if>


</j:jelly>



3 sc_cart_items:


Capture.PNG



Capture.PNG


After deleting the items:


Capture.PNG


Capture.PNG


View solution in original post

13 REPLIES 13

Mike Allen
Mega Sage

You have to use j:choose and j:when...



<j:choose>


        <j:when test="${cart_count.getAggregate('SUM','quantity')} == 0 || ${cart_count.getAggregate('SUM','quantity')} == null}">


                  0


        </j:when>


        <j:otherwise>


                  ${cart_count.getAggregate('SUM','quantity')}


        </j:otherwise>


</j:choose>



There is no if/else in jelly.


Mike



Thanks for the reply. I figured I'd have to use a combination of choose/when tags.



Here is my code; when I run the page w/ the macro, I get zippy/nada/nuttin':



  1. <g:evaluate var="cart_count" object="true">  
  2. var cart_count = new GlideAggregate('sc_cart_item');  
  3. cart_count.addAggregate('SUM','quantity');  
  4. cart_count.addQuery('active','true');  
  5. cart_count.addQuery('sys_created_by',gs.getUserName());  
  6. cart_count.setGroup(false);  
  7. cart_count.query();
  8. cart_count;
  9. </g:evaluate>


  1. <j:choose>  
  2. <j:when test="${cart_count.getAggregate('SUM','quantity')} == 0 || ${cart_count.getAggregate('SUM','quantity')} == null}">  
  3.           Number of items:       0  
  4. </j:when>  
  5. <j:otherwise>  
  6.         Number of items:         ${cart_count.getAggregate('SUM','quantity')}  
  7. </j:otherwise>  
  8. </j:choose>

Here is a screenshot:



Capture.PNG


Try this:



<g:evaluate jelly="true">    


        var cart_count = new GlideAggregate('sc_cart_item');    


        cart_count.addAggregate('SUM','quantity');    


        cart_count.addQuery('active','true');    


        cart_count.addQuery('sys_created_by',gs.getUserName());    


        cart_count.setGroup(false);    


        cart_count.query();


</g:evaluate>



<j:if test="${cart_count.next()}">


        <j:choose>    


                  <j:when test="${cart_count.getAggregate('SUM','quantity')} == 0">    


                            Number of items:       0    


                  </j:when>    


                  <j:otherwise>    


                            Number of items:         ${cart_count.getAggregate('SUM','quantity')}    


                  </j:otherwise>    


        </j:choose>


</j:if>


I am not a jelly expert but I think,  g:evaluate must have object=true

This worked for me:



<?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 jelly="true">        


  var cart_count = new GlideAggregate('sc_cart_item');        


  cart_count.addAggregate('SUM','quantity');        


  cart_count.addQuery('active','true');        


  cart_count.addQuery('sys_created_by',gs.getUserName());        


  cart_count.setGroup(false);        


  cart_count.query();


  </g:evaluate>  


  <j:if   test="${cart_count.next()}">


  <j:choose>


  <j:when test="${cart_count.getAggregate('SUM','quantity')} != 0">


  Number of items:         ${cart_count.getAggregate('SUM','quantity')}


  </j:when>


  <j:otherwise>


  Number of items:         0


  </j:otherwise>


  </j:choose>


  </j:if>


</j:jelly>



3 sc_cart_items:


Capture.PNG



Capture.PNG


After deleting the items:


Capture.PNG


Capture.PNG