How to get the count of all items in shopping cart.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 08:49 AM
I need to get the total counts of all the items in service catalog shopping cart to add to a custom template Here is the code I have attempted:
<g2:evaluate var="items_count" object="true">
var items_count = 0;
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
var item = new GlideRecord('sc_cart_item');
item.addQuery('cart', cart.sys_id);
item.query();
if(item.next()) {
while(item.next()){
items_count = items_count + item.quantity;
}
} else {
items_count = 0;
}
}
</g2:evaluate>
The problem is is that this will return 1.0. I've tried parseInt() and Math.floor as well with no luck.
I just want to get a total count of all the items in my shopping cart in service catalog.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:10 AM
You can use the glide record function getRowCount()
SO the code will look like:
var items_count = 0;
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
var item = new GlideRecord('sc_cart_item');
item.addQuery('cart', cart.sys_id);
item.query();
items_count = item.getRowCount();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:24 AM
That will only count the number of rows. If you want a sum of all quantities you need to use the GlideAggregate instead. Something like this would do:
var items_count = 0;
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
var item = new GlideAggregate('sc_cart_item');
item.addQuery('cart', cart.sys_id);
item.addAggregate('SUM', 'quantity');
item.query();
if(item.next()
items_count = item.getAggregate('SUM', 'quantity');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:29 AM
This return nothing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:31 AM
This returns 1 even with multiple different items.
var item = new GlideRecord('sc_cart_item');
item.addQuery('cart', cart.sys_id);
item.query();
items_count = item.getRowCount();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:38 AM
TRY THIS
var items_count = 0;
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
var item = new GlideRecord('sc_cart_item');
item.addQuery('cart', cart.sys_id);
item.query();
while(item.next()) {
items_count = items_count+item.quantity;
}
}