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

hwang
Kilo Contributor

I followed Brad's suggestions and it works for me.



Basically create a new script include called MyCart. Copy the entire SVN Cart code; but rename the class name to "MyCart". Also make the new script include a Global and accessible from All application scope.find_real_file.png



After that I can call it from UI actions.



Cheers,


Henry