The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Brad Bowman
Kilo Patron
Kilo Patron

The variable Auto-Populate feature, introduced with the Utah release, has been for me the most exciting new functionality in the Service Catalog space since the introduction of the multi-row variable set.  Now that I have had a chance to use it, and dig into it a little further while fielding some questions about it on the Community, there are some serious - and seemingly unnecessary - omissions to the Dependent question choices when one or both variables are part of a single-row variable set.  The good news is that one scenario has already been remedied with the Vancouver release, and the others can be accomplished with some logic improvements - in Utah and following!

 

For brevity, my use of 'variable set' for the remainder of this article will always refer to a single-row variable set, as even trying to fathom using this with a multi-row variable set makes my brain ache.

 

I. The Basic Feature - with Both Variables in a Catalog Item (not a variable set)

 

Let's start with two variables in our Catalog Item - User and Manager - both are the type of Reference, to the sys_user table.  On the Manager variable definition, we can select the User variable in the Dependent question field on the Auto-populate tab, then select the field on the referenced record, or dot-walked from it, to auto populate when the User variable is populated or changes:

BradBowman_0-1699966753630.png

Note that this also works onLoad, and the auto-populated variable will be cleared if the dependent variable is cleared!

 

II. Available in Vancouver: The Dependent Question is in a Variable Set - the Variable to be Populated is Not

 

In this example, the User variable is part of a variable set that is associated with the Catalog Item, and the Manager variable is still associated with the Catalog Item, not in the variable set.  In the Utah release, the User variable will not appear in the Dependent question reference search.  There is a Script Include used by the reference qualifier on this field that has a logic limitation which is preventing this.  Prior to upgrading to Vancouver, you can enhance this yourself by copying the text of the Script Include named SNCCatalogUtil into a new Script Include, adding a function and changing another one, then updating the reference qualifier on the Dependent question field to call your version of the Script Include.  I'll share the updated code at the end, but there are a couple of other scenarios that you may want to consider first.

 

III. Not Available in Vancouver: The Variable to be Populated is in a Variable Set - the Dependent Question is Not

 

The logic improvements to the Script Include for the Vancouver release did not cover this scenario where, following my example, the Manager variable is part of a variable set, but the User variable is not.  The User variable will not appear in the Dependent question reference search.  There is a bit of risk enabling this as a variable set can be added to more than one Catalog Item, and the definition is static, so you wouldn't want to auto-populate from a variable not in a variable set unless your variable set will only ever be used with one Catalog Item, or it will not be auto-populated in the other case(s) and may show an error.  I've seen this scenario enough to think it should be included, with the caveat that if you don't use it right it won't work - just like anything else.  Note that you may see variables in the Dependent question reference search that are not in any way associated with the intended Catalog Item (they belong to a Catalog Item associated with the same variable set(s)), but if you display both columns in the reference search, this removes uncertainty, and in my opinion, allowing the selection of the correct variable is better than not having access to this feature. 

 

IV. Not Available in Vancouver: Both Variables are in Different Variable Sets

 

That brings up the final and very plausible scenario where, again following my example, the User and Manager variables are both in different variable sets.  As the use of variable sets for Catalog Builder and general re-usability continues to expand, this is definitely something you will come across.  As of Vancouver, only the reference variables in the same variable set will appear in the Dependent question reference search.  I couldn't think of a good reason to not do this, so I did. 

BradBowman_0-1699988176867.png

In my (admittedly limited) testing, all seemed to work well selecting a value for a variable in or out of a variable set and auto-populating a variable in or out of the same or different variable set.

 

Here are the four functions that you need to add or modify in your version of the SNCCatalogUtil Script Include to make this all work.  Utah doesn't have the first three, while the Vancouver version does have the first one.

 

 

    _getSingleRowVariableSetIdList: function(catItemId) {
	    var srvsIdList = [];
	    var gr = new GlideRecord('io_set_item');
	    gr.addQuery('sc_cat_item', catItemId);
	    gr.addQuery('variable_set.type', 'one_to_one');
	    gr.query();
	    while (gr.next())
	        srvsIdList.push(gr.getValue('variable_set'));
	    return srvsIdList;
	},

	_getAllSingleRowVariableSetIdList: function(catItemIds) {
	    var srvsIdList = [];
	    var gr = new GlideRecord('io_set_item');
	    gr.addQuery('sc_cat_item', 'IN', catItemIds);
	    gr.addQuery('variable_set.type', 'one_to_one');
	    gr.query();
	    while (gr.next())
	        srvsIdList.push(gr.getValue('variable_set'));
	    return srvsIdList;
	},

	_getAllCatItems: function(variableSet) {
	    var catItemIdList = [];
	    var gr = new GlideRecord('io_set_item');
	    gr.addQuery('variable_set', variableSet);
		gr.query();
		while (gr.next())
	        catItemIdList.push(gr.getValue('sc_cat_item'));
		return catItemIdList;
	},

	getDynamicValueFieldRefQual: function(catItemId, variableSetId, variableId) {
		
		if (catItemId) {
			var srvsIdList = this._getSingleRowVariableSetIdList(catItemId);
			if (srvsIdList.length != 0)
	            return 'cat_item='+catItemId+'^ORvariable_setIN'+(srvsIdList)+'^sys_id!='+variableId+'^typeIN8,31';
	        else
	            return 'cat_item='+catItemId+'^sys_id!='+variableId+'^typeIN8,31';
	    }
	    else if (variableSetId) {
			//get cat items and other variable sets related to those cat items
			var catItemIdList = this._getAllCatItems(variableSetId);
			var srvsIdList = this._getAllSingleRowVariableSetIdList(catItemIdList);
			return 'cat_itemIN'+catItemIdList+'^ORvariable_setIN'+(srvsIdList)+'^sys_id!='+variableId+'^typeIN8,31';
      	}
	    return 'cat_item=-1';
	},

 

 

If you would like to see this enhanced functionality native in a future release, vote up my idea and/or tell your favorite ServiceNow employee!

https://support.servicenow.com/ideas?id=view_idea&sysparm_idea_id=e884b3b097a239105ad8f6e11153afbf&s... 

Comments
purdue
Kilo Sage

Hello Brad,

I have a dependent variable like in the basic feature and it is working on the catalog item however the value is not saving once the catalog item is submitted.   Is there something I am missing?

Thanks,

Chad

Brad Bowman
Kilo Patron
Kilo Patron

@purdue do you happen to have any Catalog Client Scripts or Catalog UI Policies affecting this variable, or something in the flow that is clearing it before you can see the RITM?

purdue
Kilo Sage

No I do not see anything.  I checked both I don't see anything.   I think I might convert data classification over to glide ajax to see if it is still happening.

Thanks,

Chad

purdue
Kilo Sage

Hello Brad,

I cannot get the Ajax to work.

Client Script

============================

var ga = new GlideAjax('ITSMAjaxUtils');
ga.addParam('sysparm_name', 'updatebusapp');
ga.addParam('sysparm_business_application', g_form.getValue("business_application"));
ga.getXMLAnswer(updatebusapp);
}

function updatebusapp (answer) {
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue("data_classification", returneddata.data_classification);
} else {
g_form.setValue("data_classification", clearvalue);
}
========================
Script Include
updatebusapp: function () {
var buildingid = this.getParameter('sysparm_business_application');
var loc = new GlideRecord('cmdb_ci_business_app');
loc.addQuery('sys_id', buildingid);
loc.query();
if (loc.next()){
var results = {
"data_classification": loc.getValue('data_classification')
};
return JSON.stringify(results);
 
} else {
return results;
}
},
purdue
Kilo Sage

Hello Brad,

The field I am pulling is a choice field do I need to do something different?

Thanks,

Chad

Kyle Brown
Tera Guru

In typical SN fashion they've closed the original idea due to a lack of traction even though the idea is a good one and straightforward to implement, so I'm trying again. Here's a new idea.

View Idea Page - Idea Portal

Version history
Last update:
‎11-14-2023 10:57 AM
Updated by:
Contributors