Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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;
			}

 

5 REPLIES 5

Manan Bhatt
Tera Contributor

Were you able to find a resolution on this?