logo

NJP

Enabling, Using, and Scripting the Cart - Part 2

SN Scout · Jan 22, 2021 · article

In thefirst post of this series, I discussed enabling the cart and a few use cases where users may want to take advantage of this functionality. Today we're going to discuss how we might be able to use the cart through scripts for advanced use cases.

The question one might ask is, "why would we need to script the cart?"

I would ask in return, "why do you need to buy multiple ingredients for a recipe?" When we're at the grocery store, we put multiple items in the shopping cart. Wouldn't it be great if you could select a recipe and all of the ingredients fell into the cart for us? What's more, what if the cart automatically went to the checkout, was scanned, packed, and delivered without any more manual intervention?

Let's consider two use cases for scripting the ServiceNow cart:

  1. Additional items needed to fulfill a complex order
  2. A incomplete item which needs user interaction to complete a new cart order, such as a new hire request initiated by a third-party system (e.g. HR or Identity Management)

Both of these use cases are valid reasons to do some scripting.

We have two primary methods to choose from when scripting the cart: Cart and CartJS. There are pros and cons to both, but they accomplish the same thing.

Cart is based on a Script include that you can actually access within your instance to see the inner workings. CartJS is a ServiceNow API call that is similar in function, but the details are closed to our view.

Use Case 1

For our first use case, we could use either method. An example may be that we have a new hire request where a manager has selected several entitlements. Instead of using an order guide (yuck), you can have a script open additional requested items where needed.

For Cart, we create the cart and add an item like this:

1 2 3 4 gs.include('Cart'); // must include this Script include to be able to invoke the Cart object var cart = new Cart(null, user_GlideRecord); var item = cart.addItem(gs.getProperty(Company.catalog.onboarding.item1)); // store the sys_id of the catalog item in a property instead of hard-coding cart.setVariable(item, 'variable1', 'variable1Value');

Note that for Cart, we MUST use the "gs.include('Cart')" for it to work.

For CartJS, we do it this way:

1 2 3 4 5 6 7 8 9 10 var cart = new sn_sc.CartJS('Cart Description'); var item = {}; // JSON object item item.sysparm_id = gs.getProperty(company.catalog.onboarding.item1); // sys_id of the catalog item item.sysparm_quantity = '1'; // default quantity to 1 item.variables = {}; // variables are stored in JSON item.variables.variable1 = 'variable1Value'; cart.setRequestedFor('USER sys_id'); // set our manager as the requester for this cart cart.addToCart(item); // add our newly created item from above to the cart cart.setSpecialInstructions('Here is my instruction string');

Observe that we're already using some of the CartJS API methods to set values related to the cart.

Both methods then have a checkout call to finish up and create the request and associated requested item.

1 2 3 4 5 //Checkout methods // for Cart cart.placeOrder(); // for CartJS cart.submitOrder();

Voila. Our cart is submitted.

Use Case 2

Use case 2 is unique in that we're not submitting. While we could use either method, I prefer CartJS for this one.

Say we have an external HR tool that tells ServiceNow that there is a new user, but the HR tool doesn't know what entitlements the new user requires. We can help the manager out by opening their cart and adding the new hire catalog item to it with a few prefilled values.

We open the cart as we did in the previous use case, but once we've built the item, we need to change the item's cart from our temporary cart to the default cart displayed on the portal for the user. This code is rather lengthy, with several functions so I've spelled it out on my GitHub share (linked below).

Full script samples available on my GitHub

View original source

https://snscout.blogspot.com/2021/01/enabling-using-and-scripting-cart-part-2.html