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-



Thanks for the code. I had to tweek it a little bit to work in the UI Macro 'cms_header_login':




<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>


<span style="border-radius: 10px;width:20px;height:20px; padding-left:5px;padding-right:2px;padding-top:1px;padding-bottom:1px;font-size:12px:color:white;line-height:20px;text-align: center;background:orange;font-weight:bold;font-family:Arial">


                                         


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


  <j:choose>    


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


                            0  


  </j:when>  


  <j:otherwise>        


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


          </j:otherwise>  


  </j:choose>    


  </j2:if>  


</span>



Here is what the final product looks like:


Capture.PNG



Again, appreciate everyone's helps-



Matthew


You may run into a caching issue, because all this is in phase 1, which caches.   If you do, switch the code into phase 2 by adding 2 to your g and j tags here and changing the {} brackets to [] brackets in your variables.


I have gotten it to work plenty of times without it, just doing a normal GlideRecord in the evaluate and using the variable that I declared there.



But, then again, just because I got it to work, doesn't make it best practice.


I am not sure either. The phases and the tags are quite confusing actually 😄