variables are not working with the newer sn_sc.CartJS but do work with the old Cart API

tdexter
Tera Contributor

Hi, 

 

I have created a widget with an HTML form on it, when the form is submitted it should checkout a Service Catalog item. I have it working with the older `Cart API` but I noticed in the script include for it, it states: 

This has been deprecated. Please dont use this Script Include for any usecase. ServiceNow will deactivate the Script Include in coming release. Please move to https://developer.servicenow.com/dev.do#!/reference/api/quebec/server/sn_sc-namespace/c_CartJSScoped

I tried converting it to `sn_sc.CartJS` with the documentation and adding the catalog item and checking out works, however, the variables aren't added to the request. 

 

I added a `checkbox` to the form to choose whether to use `Cart` API or `CartJS` and when using `Cart` API the variables are included on the request, when using `CartJS` they are not - all of the fields on the request are empty.

 

Any ideas why? 

 

The code is here: 

 

            var guid = gs.generateGUID(); // unique ID for the cart name
			var cartId = "cart_" + guid;
			
			if (input.depApi) { // use the deprecated cart api
				var cart = new Cart(cartId);

				// add catalog item to cart
				var item = cart.addItem('5df2d5b087b61234eb964229dabb12a6', 1);

				// fill in the variables on the request item form
				cart.setVariable(item,"first_name", input.first_name);
				cart.setVariable(item,"last_name", input.last_name);
				cart.setVariable(item,"call_back_number", input.callback_number);
				cart.setVariable(item,"alternate_email", input.alternate_email);
				cart.setVariable(item,"additional_details", input.additional_details);
				var rc = cart.placeOrder();
				gs.info('CART depApi rc {0}', JSON.stringify(rc));
			} else {
				var cart = new sn_sc.CartJS(cartId);
				var item = {
					"sysparm_id": "5df2d5b087b61234eb964229dabb12a6",
					"sysparm_quantity": "1",
					"sysparm_cart_name": cartId,
					"variables": {
						"first_name": input.first_name,
						"last_name": input.last_name,
						"call_back_number": input.callback_number,
						"alternate_email": input.alternate_email,
						"additional_details": input.additional_details
					}
				};
				gs.info('CART cartjs {0}', JSON.stringify(cart));
				var checkoutVals = cart.orderNow(item);
				gs.info('CART checkoutVals {0}', JSON.stringify(checkoutVals));
				data.checkoutVals = checkoutVals;
			}

 

4 REPLIES 4

The Machine
Kilo Sage

Have you tried using toString() for your input variables?  ie. input.first_name.toString()

Thanks for the reply! Unfortunately, it does not work. It also does not work if I hardcoded strings directly into the `item` object.

DuyDung
Tera Contributor

Can you try this code:

var guid = gs.generateGUID(); // unique ID for the cart name
			var cartId = "cart_" + guid;
			
			if (input.depApi) { // use the deprecated cart api
				var cart = new Cart(cartId);

				// add catalog item to cart
				var item = cart.addItem('5df2d5b087b61234eb964229dabb12a6', 1);

				// fill in the variables on the request item form
				cart.setVariable(item,"first_name", input.first_name);
				cart.setVariable(item,"last_name", input.last_name);
				cart.setVariable(item,"call_back_number", input.callback_number);
				cart.setVariable(item,"alternate_email", input.alternate_email);
				cart.setVariable(item,"additional_details", input.additional_details);
				var rc = cart.placeOrder();
				gs.info('CART depApi rc {0}', JSON.stringify(rc));
			} else {
                                GlideSession().get().setInteractive(false);
				var cart = new sn_sc.CartJS(cartId);
				var item = {
					"sysparm_id": "5df2d5b087b61234eb964229dabb12a6",
					"sysparm_quantity": "1",
					"sysparm_cart_name": cartId,
					"variables": {
						"first_name": input.first_name,
						"last_name": input.last_name,
						"call_back_number": input.callback_number,
						"alternate_email": input.alternate_email,
						"additional_details": input.additional_details
					}
				};
				gs.info('CART cartjs {0}', JSON.stringify(cart));
				var checkoutVals = cart.orderNow(item);
				gs.info('CART checkoutVals {0}', JSON.stringify(checkoutVals));
				data.checkoutVals = checkoutVals;
                               GlideSession().get().setInteractive(true);
			}

I believe this issue occurs when the user does not have sufficient permissions to perform the action. For Cart, you can simply set the "Available For" field to "Any User" to avoid this issue. As for CartJS, I think GlideSession().get().setInteractive(false); is a workaround for this problem.

tdexter
Tera Contributor

Thanks, I'll give it a try when I get some time.