The CreatorCon Call for Content is officially open! Get started here.

How can I order multiple items using CartJS API in a single request?

rishabhkata
Tera Contributor

I want to order more than 1 catalog item using CartJS API of ServiceNow, but its allowing only 1 Catalog item to be ordered. Can anyone look into the code below and tell me what's wrong am I doing?


var cart = new sn_sc.CartJS();
cart.empty();
var i = {
'sysparm_id': '774906834fbb4200086eeed18110c737',
'sysparm_quantity': '1'
};
var i2 = {
'sysparm_id': '04b7e94b4f7b4200086eeed18110c7fd',
'sysparm_quantity': '1'
};
cart.addToCart(i);
cart.addToCart(i2);
gs.info(JSON.stringify(cart.checkoutCart())) // getting only 1 item ordered
2 REPLIES 2

Omkar Mone
Mega Sage

Hello @rishabhkata ,

 

Can you try to use the recommended docs way to implement this? https://docs.servicenow.com/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/Cart...

Vaishnavi Lathk
Mega Sage
Mega Sage

 

To order multiple items using the CartJS API in a single request, you can modify your approach to add both items in a single call to the checkoutCart() method. However, since your original code adds items one at a time and you’re only seeing one item ordered, it's likely that the checkoutCart() is not properly accumulating items.

Here’s how you can modify your code to ensure both items are processed correctly:

 

var cart = new sn_sc.CartJS();
cart.empty();

// Create an array of items to add to the cart
var items = [
    {
        'sysparm_id': '774906834fbb4200086eeed18110c737',
        'sysparm_quantity': '1'
    },
    {
        'sysparm_id': '04b7e94b4f7b4200086eeed18110c7fd',
        'sysparm_quantity': '1'
    }
];

// Add items to the cart
for (var i = 0; i < items.length; i++) {
    cart.addToCart(items[i]);
}

// Checkout the cart
var result = cart.checkoutCart();
gs.info(JSON.stringify(result)); // Check the response for all items ordered