Leveraging the Current Catalog Item in a Variable Reference Qualifier

I recently encountered a situation where I needed a variable in a variable set but the reference qualifier needed to vary based on the catalog item that it was currently being used in. I knew of absolutely nothing that would allow me to get this, so, naturally, I did some digging. I came across this blog post that uses “current.cat_item.sys_id” to pull the catalog item into a reference qualifier. What about that makes it work? Is there a better way to do this?

To dive in, I made a simple variable set “Office Variable Set” with a single variable “Catalog Item”. This variable is a reference to the catalog item table (sc_cat_item) and has a reference qualifier of javascript:'sys_id=' + current.cat_item.sys_id to start with. I added this variable set to all of the catalog items in a baseline instance that have a Category of Office. Here’s the result:

Note that the only option in the Catalog Item variable is the current catalog item

I also found a Community article that uses current.cat_item.toString() which seems more efficient to me since ServiceNow doesn’t need to get the current.cat_item record to identify the sys_id.

Changing the reference qualifier to javascript:'sys_id=' + current.cat_item.toString() yielded the same result, as expected. So, if “sys_id” isn’t needed, is “toString()”?

Changing the reference qualifier to “javascript:’sys_id=’ + current.cat_item” also yielded the same result, as expected.

This even works on catalog items within an order guide! This should also work for the default value on a variable, but I didn’t dig into that to verify.

So, what exactly is “current” in this scenario? To find out, I adjusted the reference qualifier to this:

javascript: new getCurrentFromCatalogItem(current)

And wrote a script include that looks like this:

function getCurrentFromCatalogItem(current) {
	gs.log('CURRENT: ' + JSON.stringify(current));
}

The result of this adventure:

{
	"sys_meta": {
		"active": "1",
		"array": "0",
		"attributes": "",
		"audit": "0",
		"calculation": "",
		"choice": "1",
		"choice_field": "",
		"choice_table": "",
		"create_roles": "",
		"default_value": "",
		"delete_roles": "",
		"dependent": "",
		"dependent_on_field": "",
		"display": "sys_id",
		"dynamic_creation": "0",
		"dynamic_creation_script": "",
		"dynamic_default_value": "",
		"dynamic_ref_qual": "",
		"element_reference": "0",
		"filterable": "1",
		"foreign_database": "",
		"function_definition": "",
		"function_field": "0",
		"groupable": "1",
		"help": "",
		"hint": "",
		"i18n_sortable": "1",
		"internal_type": "collection",
		"label": "Item",
		"language": "en",
		"mandatory": "0",
		"matchable": "1",
		"max_length": "40",
		"multi_text": "0",
		"name": "sc_cart_item",
		"plural": "Items",
		"primary": "0",
		"read_only": "0",
		"read_roles": "",
		"reference": "",
		"reference_cascade_rule": "",
		"reference_floats": "0",
		"reference_key": "",
		"reference_qual": "",
		"reference_qual_condition": "",
		"reference_type": "",
		"sizeclass": "0",
		"sortable": "1",
		"spell_check": "0",
		"staged": "0",
		"sys_package": "c2e0ef8bd4040110d15219b32ee16c9e",
		"sys_scope": "global",
		"table_reference": "0",
		"text_index": "0",
		"type": "0",
		"type_description": "collection",
		"unique": "0",
		"url": "",
		"url_target": "",
		"use_dynamic_default": "0",
		"use_reference_qualifier": "simple",
		"virtual": "0",
		"widget": "",
		"write_roles": "",
		"xml_view": "0"
	},
	"variables": {
		"copier_reams": {},
		"pens": {},
		"screen_wipes": {},
		"additional": {},
		"catalog_item": {},
		"office_variable_set": {
			"catalog_item": {},
			"a66f8ae6970411107f97bfb3f153afb8": {}
		}
	},
	"variable_pool": {
		"copier_reams": {},
		"pens": {},
		"screen_wipes": {},
		"additional": {},
		"catalog_item": {},
		"office_variable_set": {
			"catalog_item": {},
			"a66f8ae6970411107f97bfb3f153afb8": {}
		}
	},
	"hierarchical_variables": {
		"copier_reams": {},
		"pens": {},
		"screen_wipes": {},
		"additional": {},
		"catalog_item": {},
		"office_variable_set": {
			"catalog_item": {},
			"a66f8ae6970411107f97bfb3f153afb8": {}
		}
	},
	"quantity": {},
	"hints": {},
	"sys_mod_count": {},
	"sc_catalog": {},
	"active": {},
	"sys_updated_on": {},
	"requested_for": {},
	"show_quantity": {},
	"sys_tags": {},
	"cart": {},
	"sys_id": {},
	"cat_item": {},
	"sys_updated_by": {},
	"sys_created_on": {},
	"sys_created_by": {},
	"order_guide": {}
}

The 6th line from the bottom indicates that the cat_item property is empty. ¯\_(ツ)_/¯

But, we can at least see what “current” is: sc_cart_item

That’s a bit of a tangent from the original purpose of this article, but it’s more proof that ServiceNow’s documentation needs work and the ecosystem needs a thriving community of bloggers, Community members, and curious people willing to dig around until they get what they need.

tl;dr: You can use current.cat_item in variable reference qualifiers to get the sys_id of the current catalog item.

Leave a Reply