Add Multiple items to cart using CartJS

Community Alums
Not applicable

I am attempting to use the CartJS API to add multiple items to a cart and checkout. Here is the code I have thus far:

var cart = new sn_sc.CartJS();
var request = {
    'special_instructions': 'Test',
    'requested_for': 'd7abd25a3b300300d901655593efc407',
    'delivery_address': "111 Main St",
};
var item1 = {
    'sysparm_id': 'a46240dddb631300818873ffbf961938',
    'variables': {
        'resource': 'McNuggets',
    }
};
var item2 = {
    'sysparm_id': 'f613e9a7db130300818873ffbf9619cd',
    'variables': {
        'test_text': 'McRib',
    }
};
cart.addToCart(item1);
cart.addToCart(item2);
cart.submitOrder(request);

While this does execute and create the REQ, it only adds the first RITM. I cannot add multiple items. Is there another way to go about this to add multiple items to the request?

Using the older 'Cart' API, I can successfully add multiple items to the cart and it creates the REQ and RITMs as expected:

var aItems = ['McNugget', 'McRib', 'McMuffin'];
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var catItem = 'a46240dddb631300818873ffbf961938';
aItems.forEach(function (item) {
    cart.addItem(catItem);
    cart.setVariable(catItem, 'resource', item);
});
var rc = cart.placeOrder();

Results in a REQ with three RITMs. Perhaps that is the better way to go about this? I was hoping to use the newer CartJS API, as it seems a little more robust.

Cheers,

Tim

7 REPLIES 7

Asbj_rn
Mega Expert

Had any luck? 

 

Pulling my hair over the same issue. 

 

First item is added just fine, the second is ignored/silent errored.

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

This will be helpful for you:

https://community.servicenow.com/community?id=community_question&sys_id=384c4b65db9cdbc01dcaf3231f9619cc

 

THanks,
Ashutosh

Thank you for the reply. 

 

unfortunately it did not help med resolve the issue of not being able to add more than one ritm.

i have not named my cart as in this the provided example. 

 

 

after the second incovation the function just returns the same cartDetails as it did previously.

calling getCartItems() also only returns one item. 

 

No Errors on the log and no exceptions thrown. 

 

This is on jakarta 04-25-2018_0907.

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

Can you tell me from where are you trying to execute this code.


Also give me your script i will try that.


Thanks,
Ashutosh