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

manikorada
ServiceNow Employee
ServiceNow Employee

Have the script as:



  1. <g:evalute var="javr_cart_count" object="true">  
  2. var cart_count = new GlideRecord('sc_cart_item');  
  3. cart_count.addQuery('active','true');  
  4. cart_count.addQuery('sys_created_by',gs.getUserName());  
  5. cart_count.query();  
  6. var count = cart_Count.getRowCount();
  7. count;
  8. </g:evaluate>  

Hi Mani,



Unfortunately this only returns the number of rows in sc_cart_item table. I need the total QUANTITY for all the rows for a specific user. Hence why i'm using glideAggregate.



Example, I have 1 record in sc_cart_item for iPhone6 with a quantity of 7. The above script will give me count = 1. I need 7



Matthew


you can use the quanity field on the sc_cart_item which will give you the quantity each item.


Mani,


Thanks for the reply.



Not sure I follow your logic here...



Matthew