CartJS.checkoutCart() creating a new DEFAULT cart

David Hadwin
Giga Contributor

When attempting to order a custom cart with the below code (within the Server Script of a Service Portal widget), ServiceNow seems to be creating a new DEFAULT cart automatically.

cartJS = new sn_sc.CartJS("test");
/* Add catalog item to cart here */
var request = cartJS.checkoutCart();

After  cartJS.checkoutCart(); runs, I look at sc_cart table and find two DEFAULT carts for my user. However, if I add a user action in between the creation and checkout so that I can slow down the process, I shows my "test" cart being created and it being ordered. So the code, works, but it seems there's a bug in the API.

Is anyone else seeing this?

Thanks,

David Hadwin

1 ACCEPTED SOLUTION

David Hadwin
Giga Contributor

I figured this out myself. It was a blunder by me. I had a sys_id reference to the cart "test" that already had been checked out, but was unbeknownst to me, updating it again. I didn't realize that passing in a non-existent sys_id to a GlideRecord would create a new cart.

var cartGR = new GlideRecord('sc_cart');
cartGR.get(cartSYS);

then at the end of my code, I was calling

cartGR.update();

and since the only thing I had inserted into this new cart was the sys_id, the name defaulted to DEFAULT.

I hope this helps someone in the future,

David Hadwin

View solution in original post

4 REPLIES 4

Willem
Giga Sage
Giga Sage

Hi David,

 

I have tried the following and that works as expected:

var cartJS = new sn_sc.CartJS("test");
var item =
{
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'carrier': 'at_and_t_mobility',
'data_plan': '500MB',
'duration': 'eighteen_months',
'color': 'slate',
'storage': 'sixtyfour'
}};
var cartDetails = cartJS.addToCart(item);
var request = cartJS.checkoutCart();

find_real_file.png

The "test"-cart contains the iPhone 5 from the script.

 

Can you try and delete the 2 default carts. Open the portal so 1 default is there. And then run the script again. See if a 2nd default is generated?

Hi Willem,

I see your "test" cart still visible, was this after checkout?

I've tried clearing all my DEFAULT carts, do you know if there's a difference between a cart with the name DEFAULT and an actual DEFAULT cart created when the cart name passed into the API is nil?

Oh sorry, I commented out the checkout to show you I had the test cart. If I do the checkout it is not showing.

If you do not specify a name it will use the Default. For me it just updates my current default (I again commented out the checkout):

find_real_file.png

 

If I remove the default cart and create test:

find_real_file.png

 

Can you see if the user on the cart is the same?

David Hadwin
Giga Contributor

I figured this out myself. It was a blunder by me. I had a sys_id reference to the cart "test" that already had been checked out, but was unbeknownst to me, updating it again. I didn't realize that passing in a non-existent sys_id to a GlideRecord would create a new cart.

var cartGR = new GlideRecord('sc_cart');
cartGR.get(cartSYS);

then at the end of my code, I was calling

cartGR.update();

and since the only thing I had inserted into this new cart was the sys_id, the name defaulted to DEFAULT.

I hope this helps someone in the future,

David Hadwin