CartJS is the scoped ServiceNow API for working with a logged-in user’s shopping cart. It lives in the sn_sc namespace, and you create an instance with new sn_sc.CartJS(). Through it you add and change items, set who the cart is for, and check out or order one item directly. It needs the Service Catalog Scoped API plugin, enabled by default.
What CartJS is and what it needs
The class gives scripts a handle on one user’s cart. You get that handle by calling the constructor, and it takes one optional argument: the name of a defined cart belonging to the current user. Leave the argument out and you work with the user’s current cart. Pass a name and you work with that cart instead. The cart is the front half of request fulfilment through the service catalog.
Two requirements sit underneath everything else. The API runs in the sn_sc namespace, so calls are written with that prefix. And the plugin with the ID com.glideapp.servicecatalog.scoped.api has to be active. It is enabled by default, so on a normal instance there is nothing to switch on.
One behaviour shapes the rest of the page. Several methods return different things depending on whether two-step checkout is enabled. A script that works against one configuration can read the wrong keys on the other, so the return shapes below are worth learning together with that switch.
Adding items and the one-instance-per-item rule
addToCart takes a map and adds a catalog item request to the cart. The map carries three things:
- sysparm_id, the sys_id of the catalog item
- sysparm_quantity, which defaults to 1
- variables, the question names mapped to answer values
The detail to get right is in the variables, the same questions an order guide or catalog form collects. The values are the stored answer values, not the labels a user sees on screen. A choice variable shows a label on the form, and the script needs the stored value behind that label.
The method returns JSON with a subtotal and an items array.
The second mistake is reusing one instance. To add several items, call sn_sc.CartJS() multiple times. A loop that builds a fresh instance on each pass and adds the same configured item twice shows the pattern. The logged result after the first pass shows one item, and after the second pass shows two, with the subtotal rising from 599.99 to 1,199.98. If you build the instance once outside the loop and call addToCart repeatedly, you are working against the pattern that adds correctly.
Changing and reading what is already in the cart
Once items are in, updateItem changes one of them. It takes two arguments: a request map and the sys_id of the cart item to modify. The map can hold sysparm_quantity (default 1), sysparm_requested_for and variables. It returns JSON describing the cart.
Note what the second argument is. It is the sys_id of the cart item, the kind of record getCartItems returns, not the sys_id of the catalog item. That is a different record, and mixing the two up is an easy way to update nothing.
For reading, several methods cover different needs:
| Method | Returns |
|---|---|
| getCartID() | The sys_id of the current cart, as a string |
| getCartDetails() | An object with the cart’s name, delivery address, requested-for user and display name, special instructions and sys_id |
| getCartItems() | A GlideRecord of the Item [sc_cart_item] records in the current cart |
| getDeliveryAddress() | The delivery address, as a string |
| getRequestedFor() | The sys_id of the User [sys_user] record the cart is requested for |
| getRequestedForDisplayName() | The display name of that user |
| getSpecialInstructions() | The special instructions, as a string |
getCartItems is the one that gives you rows to loop over. A typical use iterates the GlideRecord and logs the quantity and the catalog item for each row. Because it returns a GlideRecord rather than plain JSON, you read fields from it the way you read any other record.
getCartDetails is the quickest way to see the cart as a whole. In the sample output the cart name comes back as DEFAULT.
Setting delivery, requester and instructions
Three setters mirror three getters:
- setDeliveryAddress(address) stores a delivery address as a string.
- setRequestedFor(user) takes the sys_id of a User [sys_user] record.
- setSpecialInstructions(text) stores free text for the cart.
All three return nothing. The examples set a value and then read it back with the matching getter, which is also a sensible way to confirm in your own script that the write took effect.
There is also a role check, canViewRF(). It returns true when the current user holds the role needed to edit the Requested for field. It is called on the class, as sn_sc.CartJS.canViewRF(), rather than on an instance. It is useful before you decide whether to show or honour a requested-for change.
Checking out: three ways to finish
CartJS offers three ways to turn a cart or an item into a request, and they are easy to confuse.
checkoutCart() takes no arguments and works on the current cart. With two-step checkout enabled it returns an order summary grouped by recurring frequency, including subtotals, totals and items. With two-step checkout disabled it submits the cart and returns the request ID and request number. The sample for the disabled case returns REQ0010001.
submitOrder(request) updates cart fields and checks out in one call. The map takes special_instructions, requested_for (a sys_id, defaulting to the current user) and delivery_address. Any parameter you leave out takes its default. It returns the request ID and request number. The sample output shows REQ0000001.
orderNow(request) orders a single item without you building a cart first. The map takes:
- sysparm_id, required, the item to purchase
- sysparm_quantity, default 1
- sysparm_requested_for, defaulting to the session user
- delivery_address, defaulting to the user’s address
- special_instructions
- variables, the question names with their answer values
With two-step checkout enabled, orderNow adds the item to the cart and returns a cart ID. With it disabled, it completes the purchase and returns the request ID and request number. The sample output comes back with a cart ID, which tells you that configuration had two-step checkout on.
That return split is the practical lesson. A script that expects request_number from orderNow will find nothing when two-step checkout is on, because no request exists yet. The item is sitting in a cart waiting for the user to finish. Branch on what came back, or confirm the checkout setting first.
Finally, empty() deletes the current cart and returns nothing.
Where CartJS fits in a Service Portal build
The Service Portal already ships a cart on screen. The SC Shopping Cart widget, ID sc-shopping-cart-v2, holds a user’s orders in one place and stores its cart items against the sc_cart table. Out of the box it lets users change item quantities, add items, specify who each item is requested for, save selected items as a bundle, reload or add a saved bundle, and remove everything. It can be used as it is or cloned and modified, and it has instance options for a bootstrap colour, a cart template (the defaults are small_shopping_cart_v2.html and large_shopping_cart_v2.html) and an auto update setting that keeps the cart synchronised across sessions.
So the first question for any portal cart requirement is whether the shipped widget already does it. Where it does, CartJS is not needed.
When a requirement goes beyond the widget, the way widgets are structured decides where cart logic lives. A widget has a server script and a client script. The server script is mandatory, runs first when the widget renders for the first time, sets the initial state, runs server-side queries and sends data to the client as JSON through the data object. The client script reads that through c.data, and it sends user input back by calling server.update(), which hands the input object to the server script. The input object is undefined on first initialisation, so server code has to test for it before reading it. After server.update() runs, the client’s data object is overwritten by the server’s data object.
In practice that points to a clean split, and this is a professional reading of the structure rather than a documented recipe: the client controller collects what the user chose, such as an item and a quantity, and passes it up through input. The server script, where API calls belong, much as shared logic belongs in a script include, performs the cart operation and returns the resulting cart through data for the template to display. Keeping the cart call out of the browser keeps the logic in one place and keeps the client a thin layer.
Frequently asked questions
What is the CartJS API in ServiceNow?
It is the scoped API, in the sn_sc namespace, for working with a logged-in user’s shopping cart: adding and updating items, setting requester and delivery details, and checking out.
How do I add more than one item to the cart with CartJS?
Call sn_sc.CartJS() again for each item. Loop and build a new instance on each pass; the cart grows by one item per pass.
Why does orderNow return a cart ID instead of a request number?
When two-step checkout is enabled, orderNow adds the item to the cart and returns the cart ID. The request number comes back when two-step checkout is disabled and the purchase completes in the call.
Do I need to turn anything on to use CartJS?
The Service Catalog Scoped API plugin, ID com.glideapp.servicecatalog.scoped.api, must be active. It is enabled by default.
What do I pass in variables?
The question names mapped to the stored answer values, not the display values a user sees.
Is there a ready-made cart for Service Portal?
Yes. The SC Shopping Cart widget, sc-shopping-cart-v2, can be used as it is or cloned and modified.