Update Catalog Item short description with variable name

jyan3
Kilo Guru

Hi

I'm looking to update the short description of a catalog item so that it contains a variable from the catalog item. The ultimate goal is to display a variable when the item is added to the cart.

I'm trying to use an onSubmit Catalog Client script, but it doesn't seem to update the short description

function onSubmit() {
setValues();
}

function setValues() {
//Set Short description
var caseName = "";
caseName = "script test "+ g_form.getValue('project_name');
g_form.setValue('short_description', caseName);
}

1 ACCEPTED SOLUTION

Hey there! Basically, I do not recommend doing this, but nonetheless:

 

 

I was able to achieve this by doing the following:

 

I've opened the OOTB Portal Widget "SC Shopping Cart" and modified the server side code. If you are using a custom portal widget, this will most probably won't work (depending how much you've customized it already). Even if it is customized, you mights still get some ideas from the code.

 

The Server my final server side script:

	(function() {
		var localInput = input; //to safeguard pullution of "input" via BR or other scripts

		var m = data.msgs = {};
		m.createdMsg = gs.getMessage("Created");
		m.trackMsg = gs.getMessage("track using 'Requests' in the header or");
		m.clickMsg = gs.getMessage("click here to view");
		m.cartEmptiedMsg = gs.getMessage("Your cart has been cleared");
		m.itemRemovedMsg = gs.getMessage("has been removed from your cart");
		m.updatedMsq = gs.getMessage("Updated");
		m.quantityToMsg = gs.getMessage("quantity to");
		m.removeItemConfirmationMsg = gs.getMessage("Are you sure you want to remove this item?");
		m.clearCartConfirmationMsg = gs.getMessage("Are you sure you want to empty your cart?");
		m.removeEditingItemMsg = gs.getMessage("Are you sure you want to remove the item you are viewing from your Cart?");
		var userID = gs.getUser().getID();
		var cartName = localInput && localInput.cartName ? localInput.cartName : '';
		var cartJS = new sn_sc.CartJS(cartName, '' + userID);
		var cartItemGr = cartJS.getCartItems();
		data.isMEE = options.native_mobile == 'true';
		data.numberOfCartItems = cartItemGr.getRowCount();
		data.sys_id = $sp.getParameter('sys_id');
		data.user_id = userID;
		if (localInput && localInput.action === "checkout_two") {
			if (!isCartValid(cartJS))
				return;

			data.checkoutCartModal = getCartCheckoutModal(localInput);
			return;
		} else if (localInput && localInput.action === 'checkout') {
			checkoutCart(cartJS, cartItemGr);
			return;
		}

		var clGenerator = new GlideChoiceList();
		var choiceListQuantity = clGenerator.getChoiceList("sc_cart_item", "quantity");
		var choicelistQuantityData = [];
		for (var i = 0; i < choiceListQuantity.size(); i++) {
			var choice = choiceListQuantity.get(i);
			if (!isNaN(choice.getValue()))
							choicelistQuantityData.push({
									value: parseInt(choice.getValue()),
									label: choice.getLabel()
							});
		 }

		data.choiceListQuantity = choicelistQuantityData;

		if (localInput && localInput.action === "update_quantity") {
			var cartItemGR = new GlideRecord("sc_cart_item");
			if (cartItemGR.get(localInput.itemID) && cartItemGR.canRead()) {
				cartItemGR.setValue("quantity", localInput.quantity);
				cartItemGR.update();
			}
		}

		if (localInput && localInput.action === "remove_item") {
			cartJS.remove(localInput.removeItemID);
		}

		if (localInput && localInput.action === "clear_cart") {
			cartJS.empty(cartJS.getCartID());
		}

		if (localInput && localInput.action === "save_cart") {
			var newCart = new SPCart(localInput.newCartName, userID);
			newCart.setHidden(false);
			newCart.loadCart(cart.getCartID());
		}

		data.sys_properties = {
					twostep: gs.getProperty("glide.sc.sp.twostep", "true") == 'true'  && !cartJS.checkAllItemsHaveRequestedFor(),
					mobileNativeColor: gs.getProperty("glide.sc.mobile.primary_color", "#1f8476")
		};
		data.cart = cartJS.getCartDetails(true);
		data.cartItems = [];
		data.cart.recurring_subtotals = {};
		for(var key in data.cart.cart_items)
			if(data.cart.cart_items[key] && typeof data.cart.cart_items[key] === 'object') {
				var itemsByFrequency = data.cart.cart_items[key].items;
				data.cartItems = data.cartItems.concat(itemsByFrequency);
				if(key != 'none') {
					var show_recurring_price = false;
					for(var i=0; i<itemsByFrequency.length; i++) {
						if (itemsByFrequency[i].show_recurring_price)
							show_recurring_price = true;
					}
					if (show_recurring_price)
						data.cart.recurring_subtotals[data.cart.cart_items[key].frequency_label] = data.cart.cart_items[key].subtotal_recurring_price;
				}
			}
		data.cartItems = data.cartItems.map(function (item) {
					if (item.can_order){
							item.item_url = "?id=sc_cat_item&sys_id=" + item.sys_id + "&edit=cart";
					}
			//hey, my modifications start here
			//only do this for specific catalog item, not all
			if(item.catalog_item_id == '04e2a3042f944110cb2edebcf699b67d'){
			var gr = new GlideRecord('sc_item_option');
				//item_option_new is the Catalog Variable
				//cart item should be dynamic as it is
		gr.addEncodedQuery('item_option_new=9f1363042f944110cb2edebcf699b6b9^cart_item=' + item.cart_item_id);
		gr.query();
		if(gr.next()){

			var new_short_description = gr.value.toString();
				if(new_short_description){
				//replace short description with new value
				item.short_description = new_short_description;
													   		 }
								 }	
		}//my modifications end here
			return item;
		})



		data.disable_req_for = sn_sc.CartJS.canViewRF();

			data.saveCartModal = $sp.getWidget('widget-modal', {
					embeddedWidgetId: 'sc_save_bundle',
					embeddedWidgetOptions: {}
			});

	})();

	function checkoutCart(cart, cartItemGr) {
		try {
				var requestedCatItems = new global.GlobalServiceCatalogUtil().findCatItemsForSpLogs(cartItemGr);
				var request = cart.checkoutCart(true);
				data.result = {
					number: request.request_number,
					sys_id: request.request_id,
					table: 'sc_request'
				};
				var portalId = $sp.getPortalRecord().getUniqueValue();
				$sp.logStat('Checkout Request', 'sc_request', request.request_id, request.request_number, portalId);
				new global.GlobalServiceCatalogUtil().logRequestedCatItems(requestedCatItems, portalId);
			} catch(e) {
				var catalogExceptionUtils = new CatalogExceptionUtils();
				if(catalogExceptionUtils.isCartException(e))
					gs.addErrorMessage(e.getMessage());
			}
	}

	function getCartCheckoutModal(localInput) {
		return $sp.getWidget('widget-modal', {
				embeddedWidgetId: 'sc-checkout',
				embeddedWidgetOptions: {
					cart: localInput.cart,
					auto_redirect: localInput.auto_redirect,
					native_mobile: options.native_mobile
				},
				backdrop: 'static',
				keyboard: false,
				size: 'md'
			});
	}

	function isCartValid(cart) {
		var isValid = cart.validateCartItems();
		if (isValid != "valid") {
			gs.addErrorMessage(isValid);
			return false;
		}
		return true;
	}

 

 

View solution in original post

11 REPLIES 11

Hey there!

 

For me that view also reflects the changes we've made.

find_real_file.png


Are you sure you've copied the OOTB widget and assigned the dependencies to the same way it was configured by ServiceNow?

 

find_real_file.png

For me when i cloned the widget it didn't bring in the angular templates.

I just tried copying over the code from the other templates manually, but it didn't let me name it exactly the same

Is your widget referencing existing templates?