How to limit the total quantity of a shopping cart for a specific service catalog item in New York version?

Harriet K
Mega Guru

Hello SNow community,

I am looking to limit the total quantity of a shopping cart for a specific service catalog item only. 

Requirements:

I would like a notification to appear, where it notifies the user that the cart is already full when they attempt to add another item.
For example, the maximum items you can have in the shopping cart is 5. So when they try to add a 6th item, it will notify them that the cart is full.

This will limit the amount so that the person processing one request with multiple ritms aren’t stuck doing 20 or 100 ritms in one request. We want to be able to spread out or delegate that workload.

Is there a way to do this to a specific service catalog item only in the New York version?

I appreciate any advice that can be provided.

Thank you!
Harriet

 

3 REPLIES 3

Michael Kaufman
Giga Guru

Try adding a client script and script include

Client Script:Cart Limit
Applies to Catalog Item
Type: OnSubmit
Applies on Catalog Item View: true

function onSubmit() {
if (cartLimit(g_user.userID) == false) {
alert("Only 5 items allowed in cart");
}
}

Script Include: CartLimit
Client Callable: true
Script:
function cartLimit(userID){
    var cart = new GlideRecord('sc_cart');
    cart.addQuery('user',userID);
    cart.query();
    if (cart.next()) {
        var cartItem = new GlideRecord('sc_cart_item');
        cartItem.addQuery('cart',cart.sys_id);
        cartItem.query();
        if (cartItem.getRowCount() == 5) {
            return false;
        }
    }
}

Thanks! I will try this out.

Hi @mkaufman ,

I'm getting this error:

onSubmit script error: ReferenceError: cartLimit is not defined:
function () { [native code] }

The Client callable is enabled in the script include so I don't know why the client script is not able to recognize the script include. How do you fix this error?

Thanks!
Harriet