Cart API in UI Action

hwang
Kilo Contributor

Is it possible to use Cart API in UI Actions?

The following code not working (Cart undefined). What my other options to order a catalog item in UI Actions?

=========

var cartId = GlideGuid.generate(null);

var cart = new Cart(cartId);

var item = cart.addItem('e46305bdc0a8010a00645e608031eb0f');

cart.setVariable(item, 'os', 'Linux Red Hat');

var rc = cart.placeOrder();

gs.addInfoMessage(rc.number);

========

Thanks,

Henry

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

No it definitely won't work if it runs client side. If it happens in the scoped app I think that's the issue. The Cart script include is not available from a scoped app by default, so you have a couple of options. The first involves updating the OOB cart script include and making it available from all application scopes, but I don't think that's a great idea.



ServiceNow Cart API in Scoped Application



I think what I would do is just copy the Cart script include and create a new one in your scoped app that it can use.


View solution in original post

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would make sure that you don't have the client checkbox checked so your ui action runs server side. Also, does this happened to be part of a scoped app?


Thanks Brad,



Good point.



No, I did not have the client checkbox checked. And yes, it happens in a scoped app.



I will enable the 'client' option for this UI action. I also need a scheduled script execution to do the same thing (i.e. place an order to the cart to trigger the workflow). How can I achieve this on the server side?



Thanks,


Henry


Brad Tilton
ServiceNow Employee
ServiceNow Employee

No it definitely won't work if it runs client side. If it happens in the scoped app I think that's the issue. The Cart script include is not available from a scoped app by default, so you have a couple of options. The first involves updating the OOB cart script include and making it available from all application scopes, but I don't think that's a great idea.



ServiceNow Cart API in Scoped Application



I think what I would do is just copy the Cart script include and create a new one in your scoped app that it can use.


Hi Brad,



I have the same scenario. In my case the UI action is list choice type. So, to fetch the selected values I have made it client callable. But, as you have said Cart API wont work in this scenario.



Is there any way out? Kindly help.